sed、awk 将模式 1 行复制粘贴到以下模式 2 行中

sed、awk 将模式 1 行复制粘贴到以下模式 2 行中

我希望将 -city- 行复制到 -business- 行之后的“所有”行的末尾

这:

-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

答案1

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一行本身),打印与之前保存的行相连的当前行。

答案2

使用sed

$ sed '/^-city/h;/^-business/G;s/\n/ /' input_file
-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

答案3

for nam in -country-france -country-india -country-japan; do cont=$(sed -n "/$nam/{n;p}"  gh.txt) ;echo $nam;sed -n "/$nam/{n;p}"  gh.txt;for linnum in $(awk -v nam="$nam" '$0 ~ nam,/^$/{print NR}' gh.txt |sed -e '1,2d' -e '/^$/d'); do awk -v linnum="$linnum" -v cont="$cont" 'NR==linnum {print $0,cont}' gh.txt|sed "/^ $cont/d";done;done

输出


-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

相关内容