0

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 ?

Liso
  • 188
  • 2
  • 14
  • Don't use a range expression, use a flag as that lets you control whatever you want to print from a range - see [is-a-start-end-range-expression-ever-useful-in-awk](https://stackoverflow.com/questions/23934486/is-a-start-end-range-expression-ever-useful-in-awk). – Ed Morton Jul 21 '22 at 12:34

6 Answers6

3

Use the d command in sed to exclude lines.

sed '/# Start U23 Team/,/# End U23 Team/d' file
Barmar
  • 741,623
  • 53
  • 500
  • 612
2
awk "/# Start U23 Team/ {f=1} !f {print} /# End U23 Team/ {f=0}" file
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
2

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
HatLess
  • 10,622
  • 5
  • 14
  • 32
2

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1
awk '/# Start U23 Team/,/# End U23 Team/{next}1' file
ufopilot
  • 3,269
  • 2
  • 10
  • 12
1

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/

Daweo
  • 31,313
  • 3
  • 12
  • 25