I have a rather complicated grep and awk pattern that I use to get reports out of log files. I works great and I generate a nice light-HTML table that gets emailed to the people who need it.

I would like to add, for each section (each grep) a count of the elements in that section.

grep -c doesn't help because it only returns a count and I don't want to run the grep twice fore every log file for every section.

Everything runs inside a bash script, which also generates the HTML. I can number the rows using CSS, but getting the total count would be great.

The grep/awk is several lines long, and the end of it is the awk that generates the html:

 ...| awk '{print "<tr><td class=\"rightq\"> \
 <a href=\"mailto:admin@example.com?Subject=\ 
 "ENVIRON["DAY"]"%20"$1"&Body="ENVIRON["aBODY"]"\">" \
 $1"</a></td><td class=\"rightz\">"$2"</td><td>"$4"</td></tr>"}'
  • 1
    So, what are these "sections" you're talking about? What exactly do you want to count, and how do you want to show the results? Why do you use both grep and awk to begin with? Feb 22, 2017 at 9:35

1 Answer 1



... | awk '{ print "..." ; c++} 
  END { printf "<tr><th>%d lines</th>...</tr>",c}' 


  • c will count number of line (I assume that greping occur before the pipe)
  • END is a special awk statement run .. ath the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.