linux - How to grep to matching multiple patterns with multiple options -


i need filter text file using grep multiple patterns , multiple options:

grep -e "question" -e "query" file.txt 

this above works need add options -a

grep -e -a3 "question" -e -a5 "query" file.txt 

you can in awk simple hack grep not support that.

awk -v var1=4 -v var2=6 '/question/{while (count<var1) line[nr+count] count++;} \     /query/{count=0;while (count<var2) line[nr+count] count++; }; nr in line' file.txt 

here awk variables var1 , var2 control how lines of text including pattern needs printed. similar grep -a flags value + 1.

you can see working below sample file:-

$ cat file.txt question bc c d e f query a1 bc2 c3 d4 e5 f5 foo 

running command values 4 , 6

$ awk -v var1=4 -v var2=6 '/question/{while (count<var1) line[nr+count] count++; } \      /query/{count=0;while (count<var2) line[nr+count] count++; }; nr in line' file.txt question bc c query a1 bc2 c3 d4 e5 

this can extended number of search patterns.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -