Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep command to add end line after every match [duplicate]

do you have any idea how to add some end line like

"=========================================================================================="

after every match

tail -f error.log -n 2000 | grep -B 10 -A 25 'Exception:'

this command prints all Exceptions log but i likes to see one seperator line for each exception log.

like image 298
Sun Avatar asked Dec 13 '13 13:12

Sun


People also ask

How do you grep multiple lines after a match?

Use the -A argument to grep to specify how many lines beyond the match to output. And use -B n to grep lines before the match. And -C in grep to add lines both above and below the match!

How can I get line after grep?

Using the grep Command. If we use the option '-A1', grep will output the matched line and the line after it.

How do you grep 3 lines after a match?

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match. If you want the same number of lines before and after you can use -C num . This will show 3 lines before and 3 lines after.

How do you grep one line below?

grep's -A 1 option will give you one line after; -B 1 will give you one line before; and -C 1 combines both to give you one line both before and after, -1 does the same.


1 Answers

You want the following option:

--group-separator=SEP
Use SEP as a group separator. By default SEP is double hyphen (--).

Demo:

$ cat file
before
exception 1
after
foo
bar
before
exception 2
after

$ grep -A 1 -B 1 --group-separator======================== exception file 
before
exception 1
after
=======================
before
exception 2
after
like image 163
Chris Seymour Avatar answered Oct 25 '22 16:10

Chris Seymour