I am trying to write up a bash script to count the number of times a specific pattern matches on a list of files.
I’ve googled for solutions but I’ve only found solutions for single files.
I know I can use egrep -o PATTERN file, but how do I generalize to a list of files and out the sum at the end?
EDIT: Adding the script I am trying to write:
#! /bin/bash
egrep -o -c "\s*assert.*;" $1 | awk -F: '{sum+=$2} END{print sum}'
Running egrep directly on the command line works fine, but within a bash script it doesn’t. Do I have to specially protect the RegEx?
You could use
grep -cto count the matches within each file, and then useawkat the end to sum up the counts, e.g.: