0

I have a file that looks like that

y
z
pattern1
line
1
1
1
patern2
x
k

What I want to do is print the content between the two patterns with the following restrictions

  1. Avoid printing the patterns
  2. Skip the next line after the first pattern

This means that my output file should look like this

1
1
1

So far I am able to print between patterns, ignoring them by using

awk '/pattern1/{flag=1;next}/pattern2/{flag=0}flag' file

Any idea on how to do it?

Thanos
  • 594
  • 2
  • 7
  • 28
  • 1
    See here (fresh in print): http://stackoverflow.com/questions/38972736/how-to-select-lines-between-two-patterns/ – James Brown Aug 17 '16 at 10:50
  • @JamesBrown : Actually I got that from there, but I can't seem to be able to modify it according to my needs... – Thanos Aug 17 '16 at 10:54
  • if you okay to pipe the output, you can use different ways like `awk 'NR>1'` , `sed '1d'` , `tail -n +2` etc – Sundeep Aug 17 '16 at 12:55

3 Answers3

1

Try this:

awk '/pattern1/{i=1;next}/patern2/{i=0}{if(i==1){i++;next}}i' File
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
1
$ awk '/pattern1/,/patern2/{i++} /patern2/{i=0} i>2' file
1
1
1

Between patterns increment i, after 2 records start printing (i>2) and reset i at the end marker.

James Brown
  • 36,089
  • 7
  • 43
  • 59
0

you can record the start line number when pattern1 matched:

awk '/pattern1/{s=NR+1;p=1;next}/pattern2/{p=0}p&&NR>s' file

The next could be saved if there is no line matches both pattern1 and pattern2

Kent
  • 189,393
  • 32
  • 233
  • 301