2017-11-03

Using grep and show previous line/s of matching pattern

There was a need for me to display previous lines when it matched a specific string/pattern.  And according to MAN pages of egrep there's an option for it.  Also -n helps me show what line number the matching string is located.
# man grep

OPTIONS
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing -- between contiguous groups of  matches.

       -B NUM, --before-context=NUM
              Print  NUM lines of leading context before matching lines.  Places a line containing -- between contiguous groups of matches.

       -C NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing -- between contiguous groups of matches.

       -n, --line-number
              Prefix each line of output with the line number within its input file.

# egrep -B1 -i '2014|2015|2016' *root_pwd*.171026 -n
beid-root_pwd_check_all.ksh.171026-79-HOST: rhel6qual.corp.nn.net
beid-root_pwd_check_all.ksh.171026:80:Last password change                                      : Dec 02, 2016
--
centaur-root_pwd_check_all.ksh.171026-117-HOST: mgtsatstvm02.mgt.nnit.net
centaur-root_pwd_check_all.ksh.171026:118:Last password change                                  : Oct 02, 2015
--
dsbmgt01-root_pwd_check_all.ksh.171026-65-HOST: dsbdbadm01.dsb.mgt.nnit.net
dsbmgt01-root_pwd_check_all.ksh.171026:66:Last password change                                  : Jan 19, 2016
dsbmgt01-root_pwd_check_all.ksh.171026-67-HOST: dsbdbadm02.dsb
.mgt.nnit.netdsbmgt01-root_pwd_check_all.ksh.171026:68:Last password change                                  : Jan 19, 2016
--
dsbmgt01-root_pwd_check_all.ksh.171026-254-HOST: vpovportal
dsbmgt01-root_pwd_check_all.ksh.171026:255:Last password change                                 : Jul 29, 2016
--
uranus-root_pwd_check_all.ksh.171026-41-HOST: rout3sap.noc.nnit.net
uranus-root_pwd_check_all.ksh.171026:42:Last password change                                    : Jan 12, 2016

No comments:

Post a Comment