Awk
sed, awk 複製粘貼 pattern1 行到以下 pattern2 行
我希望將 -city-line 複製到 -business-lines 之後的“all”末尾
這個:
-country-france -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-TotalEnergies -business-AXA -business-Carrefour -business-Crédit Agricole -business-Peugeot -business-BNP Paribas -country-india -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-Reliance Industries Limited -business-State Bank of India -business-HDFC Bank -business-ICICI Bank -business-Tata Consultancy Services -business-Larsen & Toubro -country-japan -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe -business-Toyota Motor -business-Mitsubishi UFJ Financial -business-Softbank -business-Nippon Telegraph & Tel
對此:
-country-france -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-TotalEnergies -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-AXA -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-Carrefour -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-Crédit Agricole -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-Peugeot -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -business-BNP Paribas -city-Paris, Marseille, Lyon, Toulouse, Nice, Nantes -country-india -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-Reliance Industries Limited -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-State Bank of India -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-HDFC Bank -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-ICICI Bank -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-Tata Consultancy Services -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -business-Larsen & Toubro -city-New Delhi, Bangalore, Mumbai, Chennai, Kolkata -country-japan -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe -business-Toyota Motor -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe -business-Mitsubishi UFJ Financial -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe -business-Softbank -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe -business-Nippon Telegraph & Tel -city-Tokyo, Yokohama, Osaka, Nagoya, Sapporo, Fukuoka, Kobe
我在其他地方使用過的這個在這裡沒有按預期工作。感謝任何幫助。
sed -i -r ":begin;$!N;s/^-city-(.*)\n-business-(.*)/-business-\2-city-\1\n-city-\1/;tbegin;P;D" input
與
sed
:sed '/^-country/{p;d};/^-city/{h;p;d};/^$/!{G;s/\n/ /}' test_file
以多行註釋方式:
sed ' /^-country/{p;d;}; # If found a country print and go to the next line /^-city/{h;p;d;}; # If found a city save the line, print and go to the next line /^$/!{G;s/\n/ /;}; # For any, non empty lines, get the one saved before and attach it to the current line. ' file
與
awk
:awk '/^-city/{h = $0} NF && !/^-(country|city)/ {$0 = $0 " " h} 1' test
與上述基本相同
sed
。
-city
當找到以保存它開頭的行時。然後對於每一個非空行本身不是一行
-country
或-city
一行,列印目前行連接到之前保存的行。