Flash Notes
Commands - Files with no line matching a regular expression
• How to list files whose lines don't match a given regular expression ?
Solution
Remove from the file list the name of those that contain at least one line that matches the regular expression
Program
#!/bin/sh
#
# Find files whose contents do not match a regular expression
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# fmissre : File MISSing Regular Expression
#
TMP_LISTE1="/tmp/fmisstr_liste1.$$"
TMP_LISTE2="/tmp/fmisstr_liste2.$$"
if [ "$2" = "" ]; then
echo "Usage: $0 regular_expression file [file ...]"
exit 1
fi
RE="$1"
shift
# Generate the list of all files to search for
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ls -1 "$@" | sort > "$TMP_LISTE1"
# Generate the list of all files whose a line matches the regular expression
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grep -l "$RE" "$@" | sort > "$TMP_LISTE2"
# Display only the list of files in which no line matches the regular expression
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
comm -3 "$TMP_LISTE1" "$TMP_LISTE2"
# Clean temporary files
# ~~~~~~~~~~~~~~~~~~~~~
rm -f "$TMP_LISTE1" "$TMP_LISTE2"
Example 1
$ fmissre Commands page_*_US.html page_000000_US.html page_000004_US.html page_000005_US.html page_000006_US.html page_000009_US.html page_000012_US.html page_000013_US.html