stats.awk (928B) - Raw
1 #!/usr/bin/awk -f 2 3 # Beginning of a section ("part") starts with: 4 # \partNUMBER{ 5 # where NUMBER := (one | two | three | four). The ^ and $ signify line's 6 # beginning and end marks. When section begins, assign `idx` to its number. 7 /^\\partone\{$/ { idx=1; } 8 /^\\parttwo\{$/ { idx=2; } 9 /^\\partthree\{$/ { idx=3; } 10 /^\\partfour\{$/ { idx=4; } 11 12 # Section ends with "}" as a sole character in the line. 13 /^\}$/ { idx=0; } 14 15 # If idx != 0 (we are in part1,2,3 or 4), increase part's counter by the number 16 # of characters in the line. 17 idx != 0 { 18 part[idx] += length($0); 19 } 20 21 # End of the script: we have part[idx], where idx := [1,4], and the 22 # value is the number of characters. 23 END { 24 printf("number of characters in part1: %d\n", part[1]); 25 printf("number of characters in part2: %d\n", part[2]); 26 printf("number of characters in part3: %d\n", part[3]); 27 printf("number of characters in part4: %d\n", part[4]); 28 }