Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep after and before lines of last Match

I am searching through few logs and I want to grep the last match along with it's above and below few lines.

grep -A10 -B10 "searchString" my.log will print all the matches with after and before 10 lines grep "searchString" my.log | tail -n 1 will print the last match.

I want to combine both and get the after and before 10 lines of last match.

like image 828
RaceBase Avatar asked Jun 26 '14 04:06

RaceBase


People also ask

How do you grep two lines before and after?

You can use option -A (after) and -B (before) in your grep command.

How do you print 10 lines before and after grep?

You can use grep with -A n option to print N lines after matching lines. Using -B n option you can print N lines before matching lines. Using -C n option you can print N lines before and after matching lines.

How do you grep above and below the lines?

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.

How do you grep a specific line after a match?

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

How to grep the line after a line has been matched?

As said earlier the grep command by default only displays the matched lines. > grep "linux" sample.dat linux virtual server. 2. Print lines after the match. Use the -A option with grep command to print the lines after matched line.

How to show lines before and after the match in Linux?

Grep: show lines before and after the match in Linux You can add some additional parameters to your grep command, to search for keywords or phrases in files, to also show you the files that match before or after your match.

How to use grep command in Linux system?

The grep command in unix or linux system is used to print the lines that match a given pattern. By default the grep command displays only the matching lines. We can change the behaviour of the grep command to print the lines that are above and below the matched line. 1.

How to print the lines before and after the matched line?

To print the lines before the matched line use the -B option with grep command. The syntax and the example are shown below: 4. Print lines before and after match We can print both the lines above and below the matched line. Use the -a option with the grep command.


3 Answers

If you like to have all in one command, try this awk

awk '/search/ {f=NR} {a[NR]=$0} END {while(i++<NR) if (i>f-3 && i<f+3) print a[i]}' file

How it works:

awk '
/search/ {                      # Is pattern found
    f=NR}                       # yes, store the line number (it will then store only the last when all is run
    {
    a[NR]=$0}                   # Save all lines in an array "a"
END {
    while(i++<NR)               # Run trough all lines once more
        if (i>f-3 && i<f+3)     # If line number is +/- 2 compare to last found pattern, then 
            print a[i]          # Printe the line from the array "a"
    }' file                     # read the file

A more flexible solution to handle before and after

awk '/fem/ {f=NR} {a[NR]=$0} END {while(i++<NR) if (i>=f-before && i<=f+after) print a[i]}' before=2 after=2 file
like image 186
Jotne Avatar answered Oct 13 '22 12:10

Jotne


Unless I am completely misunderstanding the question, you can just tail the last 21 lines

 grep -A10 -B10  "searchString" my.log | tail -n 21

i.e.

> for i in {1..10}; do echo $i >> example; done; \
echo foo >> example; \
for i in {1..10}; do echo $i >> example; done; \
for i in {A..Z}; do echo $i >> example; done; \
for i in {a..j}; do echo $i >> example; done; \
echo foo >> example; \
for i in {k..z}; do echo $i >> example; done
> grep -A10 -B10  foo example | tail -n 21
a
b
c
d
e
f
g
h
i
j
foo
k
l
m
n
o
p
q
r
s
t
like image 41
Reinstate Monica Please Avatar answered Oct 13 '22 11:10

Reinstate Monica Please


You can try this,

tac yourfile.log | grep -m1 -A2 -B2 'search' | tac
like image 37
sat Avatar answered Oct 13 '22 12:10

sat