#148 Print lines containing words “BAR”, “GAP”, “QUAD” and count number of lines in each group using awk

Save the following awk script in the file “ex148.awk”

#!/bin/awk -f
/GAP/{print;n_gap++}
/BAR/{print;n_bar++}
/QUAD/{print;n_quad++}
END { printf "n_gap=%i\n",n_gap; printf "n_bar=%i\n",n_bar;printf "n_quad=%i\n",n_quad }

Run file “ex148run.txt”

awk -f ex148.awk test1.txt

using

./ex148run.txt

The result is

BAR 5000005      1 0.00000000 0.00000000 0.00000000 2009.00000000 0.00000000 0.00000000 0.00000000 100.00000000
GAP 5000005      1 0.00000000 0.00000000 0.00000000
QUAD 5000005      1 0.00000000 0.00000000 0.00000000 2009.00000000 0.00000000 0.00000000 0.00000000 251.00000000
n_gap=11
n_bar=14
n_quad=23