Ask Your Question

Revision history [back]

To count multiple patterns in a file using AWK, you can use the following syntax:

awk '/pattern1/ {count1++} /pattern2/ {count2++} END {print "Pattern 1 count:", count1; print "Pattern 2 count:", count2}' file.txt

In this command, replace pattern1 and pattern2 with the patterns you want to count in the file.txt. The code increments the count variables count1 and count2 every time the respective patterns are found in the file.

The END block in AWK is executed after all the lines are processed. In the END block, we print the count variables, which gives the number of occurrences of pattern1 and pattern2 in the file.

You can extend this command to count more patterns, by adding another pattern check and count variable increment in the same format.