[kwlug-disc] BASH compare items in two files

Kyle Spaans 3lucid at gmail.com
Tue Nov 2 14:37:59 EDT 2010


This would be a lot easier if you could use hashes/dictionaries
in Perl/Python!

But aside from that, a question or two: are the IDs in the ID
file unique, or do you have to worry about repeats? Will the
tag names or IDs appear on any lines that they we don't
expect? Such as strings or comments in the XML file?

If not, I'd be perfectly happy to use a couple of straight forward
command line tools to do what you want:

# remove comments
sed -ne '^#' ID-file > temp-ID-file
# find lines with the tags, then filter those by the IDs, then count
for p in foo bar baz; do
    echo "Counting $p with matching IDs:"
    grep $p data.xml | grep -f temp-ID-file | wc -l
done
rm temp-ID-file


Sounds good? :D



More information about the kwlug-disc_kwlug.org mailing list