Awk Print Line After A Matching /regex/

How do I a print line after matching /regex/ using awk under Linux / UNIX operating systems? I’d like print the line immediately after a line that matches /regex/ but not the line that matches itself.

You can use awk, sed and any other tool for the same. Here is awk syntax:
awk '/regex/ { getline; print $0 }' /path/to/file
awk -F: '/regex/ { getline; print $0 }' /path/to/file

ADVERTISEMENTS

getline used to set $0 from next input record; it also set NF, NR, FNR. For example, match ‘Registrar:’ string and print actual registrar name from whois look up:
whois bbc.co.uk | awk -F: '/Registrar:/ && $0 != "" { getline; print $0}'
Sample output:

        British Broadcasting Corporation [Tag = BBC]
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 comments… add one
  • Hai Vu May 13, 2009 @ 16:03

    I suggest to use print instead of print $0: it is shorter.

  • Nikhil May 14, 2009 @ 9:06

    $0 != “” looks like a redundant check since it is still the line matching the regex and not the next line.

  • GByte May 14, 2009 @ 9:15

    How about:
    $pgrep -A 1 regexp | tail -n 1
    ?

  • Julio A. Cartaya May 16, 2009 @ 10:17

    Simply saying:
    $ awk ‘/regexp/’ /pathto/file
    will do the trick: the default action in awk is printing.

  • nyxx May 16, 2009 @ 18:26

    Reducing awk code for a while we’ll get tha same
    $ whois bbc.co.uk | awk -F: '/Registrar:/ { getline; print }'
    British Broadcasting Corporation [Tag = BBC]

  • Ajay Jun 8, 2009 @ 3:12

    You can us grep as well:
    whois bbc.co.uk | grep -A 1 Registrar| grep -v Registrar

  • paddu Jul 1, 2011 @ 11:30

    I have a line abc=xyz
    i want to find a pattern “abc=” and if exist print xyz
    can some one please help

    • royi Jan 7, 2012 @ 0:15

      awk -F “=” ‘{if ($1 like “abc”) print $2 }’

  • Philippe Petrinko Apr 25, 2013 @ 8:50

    Hi, nice topic.

    By the way,

    1) using [-F] parameter is totally useless here, there is no point in defining [Field separator] as “:”.

    2) As Nikhil stated, test on [$0] is also useless.

    3) You may want to get rid of heading 8 space characters, using [substr]

    So this code will do the trick:

    whois bbc.co.uk | awk ‘/Registrar:/ { getline; print substr($0,9)}’

    Thanks to change your article accordingly,

    — Philippe

  • Martin Heimsoth Mar 13, 2014 @ 7:30

    Hello everyone,
    I was looking for such a solution, and I think I found something better than Philippe proposes. The thing is that very often one does not know how long the remaining string to be printed will be. So here is what I did:

    awk ‘/REGEX/ {gsub(“REGEX”,””);print;}’ /path/to/file

    Note that the regular expression in the first argument of gsub has to be the same as the search pattern, except for some special characters which might need an escape sequence.

    Sorry for showing up so late :)

    – Martin

  • Philippe Petrinko Mar 13, 2014 @ 8:51

    @Martin Heimsoth

    I would be glad to learn something new and better than solution I know, but I don’t get your point.

    I think you may have not understand what I coded.
    My solution does not depend on length of remaining string.

    ” substr($0,9) ” cuts a line from 8 heading characters, and this will work as designed whatever length of remaining string may be.

    There is no problem regarding length of printed line, CMIIAW

    — Philippe

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.