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.
You can use option -A (after) and -B (before) in your grep command.
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.
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.
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.
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.
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.
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.
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.
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
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
You can try this,
tac yourfile.log | grep -m1 -A2 -B2 'search' | tac
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With