Unix command for extracting lines befor and after a particular searched string pattern
11,051
Solution 1
For three lines before and after the match
grep -C 3 pattern filename
For more control on number of after and before lines to be displayed for a match, use
grep -A (num of after) -B (num of lines before) pattern filename
From man grep
:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of matches.
-a, --text
Process a binary file as if it were text;
this is equivalent to the --binary-files=text option.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of matches.
-C NUM, --context=NUM
Print NUM lines of output context.
Places a line containing -- between contiguous groups of matches.
Solution 2
Grep has options to display rows immediately before and after the match. The numbers in the command line below are the appropriate number of rows to display, after and before the match. E.g.
grep -A3 -B5 yoursearchpattern inputfilepattern
man grep is useful for details about the options.
Assuming you have GNU grep, to check you can use --version option:
> grep --version
GNU grep 2.6.3
Author by
wanted
Updated on June 29, 2022Comments
-
wanted almost 2 years
How can I search for lines in a file and extract the lines above and below lines of the searched line .
My input is like
Tue Jun 26 14:59:46 2012 Warning ffffffff act_msg_ctms_remove_from_pending_queue: deleting message 44817201 from the queue. Tue Jun 26 14:59:46 2012 Warning ffffffff Finishing processing record number 44817201 Tue Jun 26 14:59:46 2012 Warning 5000000 activity_queue_manager_finish_cb: unknown activity 120. Tue Jun 26 14:59:46 2012 Warning ffffffff Activity State Machine priority (2) finished Tue Jun 26 14:59:46 2012 Warning ffffffff ==================================================== Processing database file "INCOMING_MESSAGES" record number 47810234 from user "(unknown)" Tue Jun 26 14:59:46 2012 Warning ffffffff ACTIVITY data: rec_num (47810234) size (116) Tue Jun 26 14:59:46 2012 Warning ffffffff activity status: ACT_SENT Tue Jun 26 14:59:46 2012 Warning ffffffff MESSAGE body "MVT QFA6673/26.VHQOS.BNE EA0541 " Tue Jun 26 14:59:46 2012 Warning ffffffff Finishing processing record number 47810234 Tue Jun 26 14:59:46 2012 Warning ffffffff Activity State Machine priority (1) finished Tue Jun 26 14:59:46 2012 Warning ffffffff End processing record number 47810234
====================================================
And I require my output to be like
/ Tue Jun 26 14:59:46 2012 Warning ffffffff MESSAGE body "MVT QFA6673/26.VHQOS.BNE EA0541" /
My search string would be MVT.
Pls help
-
jpe almost 12 yearsThanks, +1 for the -C option, been using A and B, learned a new letter today!
-
pyfunc almost 12 years@jpe: But using A and B was the correct answer to the questions since his context lines for after and before differs. :)
-
wanted almost 12 yearsIn both of these cases its throwing up an error called illegal option
-
pyfunc almost 12 years@wanted: This is for gnu grep. Which grep are you using?