1

I am trying to print lines between two patterns through sed command. But I want to include the line containing Pattern1 in the result and exclude the Pattern2. For ex:

/PAT1/
line 1
line 2
line 3
/PAT2/

The desired output is :

/PAT1/
line 1
line 2
line 3

I have tried this :

sed -n '/PAT1/,/PAT2/{/PAT2/{d};p}' Input_File

But it is excluding both the patterns.

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

1

You can do it with awk: awk '/patt1/{flag=1}/patt2/{flag=0}flag' input_file

If input_file is:

111 
222 
333 
444 
555

awk '/222/{flag=1}/444/{flag=0}flag' input_file

gives:

222
333
John3136
  • 28,809
  • 4
  • 51
  • 69