awk adjustment to print total

im trying to print all lines in the /var/log/syslog file that contain the pattern CRON. and after all the lines have been printed, i want a total of all the lines that contained "CRON" to be printed at the end.

the below command is printing the correct lines, but it is giving me the sum of all lines in the syslog file, instead of only giving a total of lines that contained CRON


awk "BEGIN{count=0} /CRON/ {print} { count++ }  END { print count }" /var/log/syslog

Correct your code like below :

awk '/CRON/{print; count++ }END { print count }'  /var/log/syslog
1 Like