How do you grep a file and get the next 5 lines How do you grep a file and get the next 5 lines shell shell

How do you grep a file and get the next 5 lines


You want:

grep -A 5 '19:55' file

From man grep:

Context Line Control-A NUM, --after-context=NUMPrint NUM lines of trailing context after matching lines.  Places a line containing a gup separator (described under --group-separator) between contiguous groups of matches.  With the -o or --only-matchingoption, this has no effect and a warning is given.-B NUM, --before-context=NUMPrint NUM lines of leading context before matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or --only-matchingoption, this has no effect and a warning is given.-C NUM, -NUM, --context=NUMPrint NUM lines of output context.  Places a line containing a group separator(described under --group-separator) between contiguous groups of matches.  With the -o or --only-matching option,  this  has  no effect and a warningis given.--group-separator=SEPUse SEP as a group separator. By default SEP is double hyphen (--).--no-group-separatorUse empty string as a group separator.


Some awk version.

awk '/19:55/{c=5} c-->0'awk '/19:55/{c=5} c && c--'

When pattern found, set c=5
If c is true, print and decrease number of c


Here is a sed solution:

sed '/19:55/{NNNNNs/\n/ /g}' file.txt