简体   繁体   中英

Bash print all line except start to end pattern

I have this input file with list of player names.

Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
# Start U23 Team
Alvaro Fernandez
Zidane Iqbal
Facundo Pellistri
Hannibal Mejbri
Charlie Savage
# End U23 Team
Harry Maguire
Lisandro Martinez
Alex Telles

I want to exclude line starting with Start U23 Team and end with End U23 Team , so the end result will be:

Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
Harry Maguire
Lisandro Martinez
Alex Telles

I can print the U23 Player with this command awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file , but how to do vice versa ?

使用sed中的d命令排除行。

sed '/# Start U23 Team/,/# End U23 Team/d' file
awk "/# Start U23 Team/ {f=1} !f {print} /# End U23 Team/ {f=0}" file
awk '/# Start U23 Team/,/# End U23 Team/{next}1' file

Using sed

$ sed -z 's/# Start.*# End U23 Team\n//' input_file
Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
Harry Maguire
Lisandro Martinez
Alex Telles

With GNU awk , please try following code, written and tested with shown samples only. Simply making use of RS variable where setting it to regex '(^|\n)# Start U23 Team\n.*\n# End U23 Team\n as per requirement and then in main program simply printing the lines.

awk -v RS='(^|\n)# Start U23 Team\n.*\n# End U23 Team\n' '{sub(/\n$/,"")} 1' Input_file

I can print the U23 Player with this command awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file , but how to do vice versa ?

This code might be reworked following way

awk 'BEGIN{f=1} /# Start U23 Team/{f=0} f; /# End U23 Team/{f=1}' file

Explanation: BEGIN pattern allows executing action before starting processing lines, here I set f value to 1 , /# Start U23 Team/ was swapped with /# End U23 Team/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM