-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
"save"로 시작하는 줄이 발견된 경우.
그런 다음 비어 있지 않은 각 줄(줄 -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