I am trying to grep certain pattern from a file and list it but grep command is not working consistently.
For one of the pattern its working and for other its not working. Please let me know if I am missing anything:
The file to grep
=============================================================================================================================== START_TIME END_TIME CLIENT_NAME STATUS BACKUP_TYPEPOLICY_NAME =============================================================================================================================== 2015-12-09 01:51:35 2015-12-09 02:11:47 atrcxb1144-bup3 success FULL atrcxb1144-bup3_FILES 2015-12-09 02:13:06 2015-12-09 02:14:12 atrcxb1144-bup3 success FULL atrcxb1144-bup3_Hot_Catalog 2015-12-15 08:17:48 2015-12-15 08:18:55 atrcxb1144-bup3 success FULL atrcxb1144-bup3_Hot_Catalog 2016-01-23 23:55:00 2016-01-24 00:17:03 cbtcnbgrn2eniqs2-bkup success FULL ENIQ_STATS_ROOT_cbtcnbgrn2eniqs2-bkup
Unsuccessful
bash-3.2# /usr/sfw/bin/gegrep '([[:blank:]]+success[[:blank:]]+[FI][UN][LC][LR][[:blank:]]+ENIQ_STATS_ROOT_cbtcnbgrn2eniqs2-bkup[[:blank:]]+)' /usr/openv/netbackup/db/.backup_history
bash-3.2# echo $?
1
Successful
bash-3.2# /usr/sfw/bin/gegrep '([[:blank:]]+success[[:blank:]]+[FI][UN][LC][LR][[:blank:]]+ENIQ_STATS_ONBLADE_RAW_cbtcnbgrn2eniqs2-bkup[[:blank:]]+)' /usr/openv/netbackup/db/.backup_history
2016-01-23 23:55:00 2016-01-24 00:17:03 cbtcnbgrn2eniqs2-bkup success FULL ENIQ_STATS_ONBLADE_RAW_cbtcnbgrn2eniqs2-bkup
bash-3.2# echo $?
0
It seems like in the unsuccessful case, ENIQ_STATS_ROOT_cbtcnbgrn2eniqs2-bkup
is not followed by a space/blank, while in the successful one, ENIQ_STATS_ONBLADE_RAW_cbtcnbgrn2eniqs2-bkup
is.
And your regex matches a required blank at at the end of those two columns.
So grep is working as expected, and to match the string in your unsuccessful case, you need to replace the last [[blank]]+
with [[blank]]*
as follows:
bash-3.2# /usr/sfw/bin/gegrep '([[:blank:]]+success[[:blank:]]+[FI][UN][LC][LR][[:blank:]]+ENIQ_STATS_ROOT_cbtcnbgrn2eniqs2-bkup[[:blank:]]*)' /usr/openv/netbackup/db/.backup_history
The +
matches one or more occurrences of the previous pattern, while *
zero or more occurrences.