Data:
Pattern Data Data Data
Data Data Data
Data Data Data
...
With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line.
Data:
Pattern Data Data Data
Data Data Data
Data Data Data
...
With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line.
$ cat input.txt
first line
something else
Pattern Data Data Data
Data Data Data
Data Data Data
...
$ sed -n "/Pattern/,$ p" input.txt
Pattern Data Data Data
Data Data Data
Data Data Data
...
That sed command works great from command line, but when I put that pattern into an awk script, it only prints the pattern line. It does not print the subsequent lines. Can you tell me how to do this same thing in awk?
what behaviour do you expect in case you have a file containing lines like :
pattern
pattern
blabla
blabla
should the first blabla be displayed ?
I would like it to stop after the data lines:
Pattern data data data
data data data
data data data
...
blahblahblah (another pattern match to stop printing?)
To print the line containing pattern and the next following line :
awk '/pattern/{c=3}{c>0?c--:c=0}c' myfile
or
awk '{y=x;x=($0~/pattern/)?1:0}x||y' myfile
or
awk '/pattern/{c=3}--c>0' myfile
I want to match on pattern, print subsequent lines, then do NOT print from blahblah to EOF.
Pattern data data data
data data data
data data data
...
blabla <-- do not print to EOF. If first blabla must be printed, that's okay.
# cat myfile
1 whatever
2 blabla toto titit
3 blabla toto titit apattern
4 blabla toto titit
5 blabla toto titit
6 blabla toto titit pattern
7 blabla pattern toto titit
8 blabla toto titit
9 blabla toto titit
# awk '/pattern/{c=3}--c>0' myfile
3 blabla toto titit apattern
4 blabla toto titit
6 blabla toto titit pattern
7 blabla pattern toto titit
8 blabla toto titit
#
It may be better to use
awk '/pattern/{c=3}c-->0' myfile
instead of
awk '/pattern/{c=3}--c>0' myfile
The number 3
will then reflect the total line printed.
Is this what you are looking for:
$ cat input
Pattern data data data
data data data
data data data
...
blabla
xxx xxx
yyy
$ sed -n "/Pattern/,/blabla/ { /blabla/d; p }" input
Pattern data data data
data data data
data data data
...
$ sed -n "/Pattern/,/blabla/ p" input | head -n -1
Pattern data data data
data data data
data data data
...