i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made.
Hi,
Try this one,
awk -v pat="YourPatternStr" -v cnt=6 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){print a;if(a ~ pat ){i=i+cnt;}}}' file
Cheers,
Ranga:)
Hi ,
It works but i am trying something like this.. and it removes the original file. why so ?
ssh $i "/usr/xpg4/bin/awk -v pat="CMDS=" -v cnt=6 '{a[NR]=$0;}END{for(i=1;i<=NR;i++){print a;if(a ~ pat ){i=i+cnt;}}}' sudofile > sudofilecopy"
when i check it empties sudofile content
$ cat -n a.txt
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
$ awk '/two/{for(i=0;i<=5;i++){getline}}1' a.txt
one
eight
nine
ten
eleven
i would like to to include the matched pattern line as well in the output . how to achieve that
$ awk '/two/{print;for(i=0;i<=5;i++){getline}}1' a.txt
one
two
eight
nine
ten
eleven
can you please provide some explanation of your oneliner.. i am trying to learn awk.
---------- Post updated at 10:19 AM ---------- Previous update was at 10:17 AM ----------
rangarasan .. sorry that was problem with my script that nullified the file.
---------- Post updated at 10:19 AM ---------- Previous update was at 10:19 AM ----------
rangarasan .. sorry that was problem with my script that nullified the file.
$ awk '
# Run this code block whenever we see a line containing the string 'two'
/two/{
# Print the entire unmodified line
print;
# Read 6 more lines.
for(i=0;i<=5;i++){getline}
# Print the very last of the 6, but not the 5 before.
}1' a.txt
awk 'c-->0{next} $0~pat{c=6}1' pat=two infile
The awks with getline : watch what happens when you look for "nine" for example...