файл summarize.awk
:
function print_feature() { if (feature) print feature ": " n n = 0 feature = "" } NF == 0 { # empty line. print_feature() # print the feature summary in_feature = 0 # we are no longer counting elements next # do not print the empty line } $1 == "Feature" { # a new feature print_feature() # print the previous feature summary feature = $0 # save this as the new feature in_feature = 1 # indicate we are counting elements next # do not print ... yet } { if (in_feature) n++ # count this element else # or print # print (e.g. "Sample") } END { print_feature() # if there is no trailing blank line, print the current feature }
затем
$ awk -f summarize.awk file Sample1 Feature 1: 4 Feature 2: 3 Sample2: Feature 1: 2 Feature 2: 3