Skip to content

Instantly share code, notes, and snippets.

@sdmcraft
Last active February 2, 2018 06:01
Show Gist options
  • Save sdmcraft/a550676194e6814be30e956810b4fdb4 to your computer and use it in GitHub Desktop.
Save sdmcraft/a550676194e6814be30e956810b4fdb4 to your computer and use it in GitHub Desktop.
Find the counts of a pattern across files in a folder
#!/bin/sh
usage()
{
echo -e "Usage: $0 -i <input folder path containing files to search> -j <file name pattern> -p <pattern to search> -e <pattern to exclude from search results> -l <Character to compare for uniqueness> -f <Number of prefix fields to ignore while printing matching lines" \
"\nExample Usage: ./count-pattern.sh -i /mnt/installation/crx-quickstart/logs -j *log* -p *ERROR* -e 'Non-existant pattern' -l 25 -f 6" 1>&2; exit 1;
}
while getopts i:j:p:e:l:f: option
do
case "${option}"
in
i) SOURCE_FOLDER=${OPTARG};;
j) FILE_NAME_PATTERN=${OPTARG};;
p) PATTERN=${OPTARG};;
e) EXCLUDE_PATTERN=${OPTARG};;
l) COMPARE_LENGTH=${OPTARG};;
f) IGNORE_FIELDS=${OPTARG};;
*) usage;;
esac
done
for FILE in $SOURCE_FOLDER/$FILE_NAME_PATTERN
do
OUTPUT=`grep "$PATTERN" $FILE | grep -v "$EXCLUDE_PATTERN" | wc -l`
if [ "$OUTPUT" -eq "0" ];then
continue
fi
echo $FILE:$OUTPUT
grep "$PATTERN" $FILE | grep -v "$EXCLUDE_PATTERN" | cut -d ' ' -f $IGNORE_FIELDS- | sort | uniq -c -w $COMPARE_LENGTH
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment