Skip to main content
2 of 2
don't reinvent the wheel; much less do so badly
terdon
  • 252.9k
  • 69
  • 481
  • 720

And here's an awk approach:

$ awk -F'[ ,]' -v RS= '{n=split($0,a,"\n"); print $2,n-1}' file [email protected] 1 [email protected] 2 [email protected] 0 [email protected] 1 

Explanation

  • -F'[ ,]': set the field separator to space or comma.
  • -v RS=: enables paragraph mode, where records are separated by one or more consecutive blank lines.
  • n=split($0,a,"\n");: split the current record (paragraph) on \n into the array a. The number returned (n) is the number of elements in this array, so the number of \n characters in this record, and therefore the number of attributes plus one.
  • print $2,n-1: print the second field (since we are using space and = as the field separator, on your file this will be the string after the first space and before he first ,), and the value of n minus one, so the number of lines minus one.
terdon
  • 252.9k
  • 69
  • 481
  • 720