UNIX Script for a list of strings find those not in any file
I'm parsing a properties file to get a list of properties defiend. I want to check all the places these properties are used (target d开发者_开发知识库ir and subdirs), flagging up any that are defined in the properties file but not used anywhere in the targer dir. Thus far I have
FILE=$1
TARGETROOT=$2
for LINE in `grep '[A-Z]*=' $FILE | awk -F '=' '{print$1}'`;
do
done;
Inside this loop I want to find those $LINE vars which are not in $TARGETROOT or its subdirs
Example files
Properties File
a=1
b=2
c=3
...
Many files that contain references to properties via
FILE 1
PropAValue = a
check the return code of grep.
You can do this by inspecting the $? variable.
if it is 0 then the string was found, otherwise the string was not found. If not 0 then add that string to a 'not found' array and that should be your list of not found properties.
grep "string"
if [$? -ne 0]
then
string not found
fi
- Using
xyz | while read PROP
instead offor PROP in ``xyz``; do
for those cases whenxyz
can get arbitrarily large Using
grep -l ... >/dev/null || xyz
to executexyz
ifgrep
fails to match, and discard thegrep
output do/dev/null
without executingxyz
if one match is found (-l
stopsgrep
after the first match, if any, making it more efficient)FILE=$1 TARGETROOT=$2 grep '^[A-Z]*=' "$FILE2" | awk -F= '{print$1}' | while read PROP ; do find "$TARGETROOT" -type f | while read FILE2 ; do grep -l "^${PROP}=" "$FILE2" >/dev/null || { echo "Propery $PROP missing from $FILE2" } done done
If dealing with a large number of properties and/or files under $TARGETROOT
you can use the following, more efficient approach (which opens and scans each file only once instead of the previous solution's N
times, where N
was the number of properties in $FILE
):
- Using a temporary file with all sorted properties from
$FILE
to avoid duplicating work - Using
awk ... | sort -u
to isolate all sorted properties appearing in another file$FILE2
Using
comm -23 "$PROPSFILE" -
to isolate those lines (properties) which only appear in$PROPSFILE
and not on the standard input (i.e. in$FILE2
)FILE=$1 TARGETROOT=$2 PROPSFILE="/tmp/~props.$$" grep '^[A-Z]*=' "$FILE" | awk -F= '{print$1}' | sort -u >"$PROPSFILE" find "$TARGETROOT" -type f | while read FILE2 ; do grep '^[A-Z]*=' "$FILE2" | awk -F= '{print$1}' | sort -u | comm -23 "$PROPSFILE" - | while read PROP ; do echo "Propery $PROP missing from $FILE2" done done rm -f "$PROPSFILE"
精彩评论