Quantcast
Channel: Grep for multiple strings, show number of lines after one of the strings (but not the other) - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Grep for multiple strings, show number of lines after one of the strings (but not the other)

$
0
0

I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.

cat file | grep -e 'string1' -e 'string2'

works of course, but I would like to get something like

cat file | grep -e 'string1' -e -A 5 'string2'

How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?

EDIT: just got it working with awk:

cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>