271
grep -A1 'blah' logfile

Thanks to this command for every line that has 'blah' in it, I get the output of the line that contains 'blah' and the next line that follows in the logfile. It might be a simple one but I can't find a way to omit the line that has 'blah' and only show next line in the output.

codeforester
  • 39,467
  • 16
  • 112
  • 140
facha
  • 11,862
  • 14
  • 59
  • 82

14 Answers14

215

you can try with awk:

awk '/blah/{getline; print}' logfile
Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
208

If you want to stick to grep:

grep -A1 'blah' logfile | grep -v "blah"

or alternatively with sed:

sed -n '/blah/{n;p;}' logfile
mias
  • 3
  • 3
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 3
    @Kent, thanks for the tip. From my POV though, grep is much more readable and easy to grasp compared to sed or the awk answer marked as best answer....but it's just me maybe :) – icasimpan Sep 16 '14 at 06:35
  • 4
    For those curious about what -v does: -v --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) – Sámal Rasmussen Feb 08 '17 at 09:47
  • 3
    I really like this answer, but it won't work if you have two lines in a row containing "blah" and want to get the second one (because it's "the next line after the matched one"). I can't think of any use case for that off the top of my head, but it's worth noting. – Tin Wizard Aug 02 '18 at 18:45
  • The top solution is merely "ok". If the second line happens to have "blah" in it as well, you'll have a real mess on your hands. – riwalk Mar 24 '19 at 00:29
  • 1
    WARNING: Assumes you do not get two matching lines next to each other. The "grep will then have extra "--" lines, while the "sed" skips lines. BUT I find this "sed" solution the most useful when that is not the case. – anthony Jan 05 '21 at 00:30
  • the "sed" option is pretty useful for many use cases – glwhart Apr 08 '21 at 13:38
39

Piping is your friend...

Use grep -A1 to show the next line after a match, then pipe the result to tail and only grab 1 line,

cat logs/info.log | grep "term" -A1 | tail -n 1
martemiev
  • 418
  • 4
  • 10
weisjohn
  • 793
  • 5
  • 9
14

Great answer from raim, was very useful for me. It is trivial to extend this to print e.g. line 7 after the pattern

awk -v lines=7 '/blah/ {for(i=lines;i;--i)getline; print $0 }' logfile
souter
  • 386
  • 4
  • 17
13

Many good answers have been given to this question so far, but I still miss one with awk not using getline. Since, in general, it is not necessary to use getline, I would go for:

awk ' f && NR==f+1; /blah/ {f=NR}' file  #all matches after "blah"

or

awk '/blah/ {f=NR} f && NR==f+1' file   #matches after "blah" not being also "blah"

The logic always consists in storing the line where "blah" is found and then printing those lines that are one line after.

Test

Sample file:

$ cat a
0
blah1
1
2
3
blah2
4
5
6
blah3
blah4
7

Get all the lines after "blah". This prints another "blah" if it appears after the first one.

$ awk 'f&&NR==f+1; /blah/ {f=NR}' a
1
4
blah4
7

Get all the lines after "blah" if they do not contain "blah" themselves.

$ awk '/blah/ {f=NR} f && NR==f+1' a
1
4
7
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    This is a good solution that handles consecutive matching lines, in the two possible ways... Up voted for completeness! – anthony Jan 05 '21 at 00:45
12

If that next lines never contain 'blah', you can filter them with:

grep -A1 blah logfile | grep -v blah

The use of cat logfile | ... is not needed.

jww
  • 97,681
  • 90
  • 411
  • 885
ott--
  • 5,642
  • 4
  • 24
  • 27
6

In general, I agree you're asking a lot of grep here, and that another tool may be the better solution. But in an embedded environment, I may not want to have sed or awk just to do this. I found the following solution works (as long as they're not contiguous matches):

grep -A1 AT\+CSQ wvdial.out | grep -v AT\+CSQ

Basically, match them, appending 1 line of context for each match, and then pipe that through an inverse match of your original pattern to strip those out. This of course means you can assume that your pattern doesn't show up in the "next" line.

Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
6

I don't know of any way to do this with grep, but it is possible to use awk to achieve the same result:

awk '/blah/ {getline;print}' < logfile
jww
  • 97,681
  • 90
  • 411
  • 885
raimue
  • 4,322
  • 1
  • 21
  • 31
  • @jww There is no difference. As you can see from the timestamps that are just minutes apart, it appears that I answered around the same time. – raimue Feb 22 '18 at 19:06
4

perl one-liner alert

just for fun... print only one line after match

perl -lne '$. == $next && print; $next = ($.+1) if /match/' data.txt

even more fun... print the next ten lines after match

perl -lne 'push @nexts, (($.+1)..($.+10)) if /match/; $. ~~ @nexts && print' data.txt

kinda cheating though since there's actually two commands

filimonov
  • 1,666
  • 1
  • 9
  • 20
beasy
  • 1,227
  • 8
  • 16
  • Can you explain $.==$next? Clearly it's more than a numeric comparison of the current line number with $next, but I don't understand how/why it works. – Keith Bentrup Mar 27 '19 at 11:40
  • It's not any more than that. `$. == $next && print` is the same as `print if $. == $next` – beasy Mar 27 '19 at 16:27
  • Ah, I see now. Thx! Of the 2 parts, each is true and executes on separate, adjacent iterations of the loop. I suppose if the point was to print any line after any match that you would want to reverse the order (would behave more like `grep -A1`). If you want to print only next lines but never a line with a match, then you would keep it this order. (just noting how $next would be clobbered for consecutive matches) – Keith Bentrup Mar 28 '19 at 12:29
3

It looks like you're using the wrong tool there. Grep isn't that sophisticated, I think you want to step up to awk as the tool for the job:

awk '/blah/ { getline; print $0 }' logfile

If you get any problems let me know, I think its well worth learning a bit of awk, its a great tool :)

p.s. This example doesn't win a 'useless use of cat award' ;) http://porkmail.org/era/unix/award.html

slm
  • 15,396
  • 12
  • 109
  • 124
sillyMunky
  • 1,260
  • 8
  • 13
2

you can use grep, then take lines in jumps:
grep -A1 'blah' logfile | awk 'NR%3==2'

you can also take n lines after match, for example:
seq 100 | grep -A3 .2 | awk 'NR%5==4'
15
25
35
45
55
65
75
85
95
explanation -
here we want to grep all lines that are *2 and take 3 lines after it, which is *5.
seq 100 | grep -A3 .2 will give you:
12
13
14
15
--
22
23
24
25
--
...
the number in the modulo (NR%5) is the added rows by grep (here it's 3 by the flag -A3), +2 extra lines because you have current matching line and also the -- line that the grep is adding.

lisrael1
  • 348
  • 2
  • 7
0

grep /Pattern/ | tail -n 2 | head -n 1

Tail first 2 and then head last one to get exactly first line after match.

0

You can match multiple lines and output n lines after with:

awk 'c&&!--c;/match1/{c=1};/match2/{c=2}' file

Match multiple items and output n lines after:

#!/bin/sh
[ -z $3 ] && echo $0 file \[match n\]+... \# to output line n after match && exit
f=$1; shift; while [ \! -z $2 ]; do s="$s/$1/{c=$2};"; shift; shift; done
awk 'c&&!--c;'$s $f

So you can save the above as matchn.sh and chmod +x matchn.sh and then if you have a file like this:

line1
line2 
line3
line4
line5
line6
line7

And run:

./matchn.sh file line1 1 line3 2 line6 1

You will get:

line2
line5
line7

This will need a bit more tweaking to support 0 offsets.

dagelf
  • 1,468
  • 1
  • 14
  • 25
0

With grep you could output the line number (-n). By that the output differs between the match and the next line through <num>: and <num>-:

# grep -A1 -n '230 Login successful."$' /var/log/vsftpd.log
1:Sat Nov  5 03:29:43 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "230 Login successful."
2-Sat Nov  5 03:29:43 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "221 Goodbye."
3:Sat Nov  5 04:44:41 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "230 Login successful."
4-Sat Nov  5 04:44:42 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "221 Goodbye."

By that we are able to filter the output to get only the next line of each match:

# grep -A1 -n '230 Login successful."$' /var/log/vsftpd.log | grep -E "^[0-9]+-" | cut -d"-" -f2-
Sat Nov  5 03:29:43 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "221 Goodbye."
Sat Nov  5 04:44:42 2022 [pid 10] [foo] FTP response: Client "10.1.1.1", "221 Goodbye."
mgutt
  • 5,867
  • 2
  • 50
  • 77