One way to do this is to somewhat reverse the idea of which is the input and use a.txt
as the patterns to search for and what you're calling "input" (I'll call "file2") to be what is searched in:
grep -o -f a.txt file2
or
echo "/abc/dog" | grep -o -f a.txt
These won't output anything for "/dog", although the echo
version will have a non-zero return code.
Edit:
This will more closely match your requested output:
while read -r line
do
match=$(echo "$line" | grep -of a.txt)
match=${match:-(NONE)}
printf "%-12s => %s\n" "$line" "$match"
done < file2
You can force the search patterns to start at the beginning of the line like this:
grep -o -f <(sed 's/^/^/' a.txt) file2
I think that the query
/abc/dog
should return/abc/d
. – mrucci – 2010-12-13T20:27:05.6771@mrucci: I believe the desired result is that the input should match the entire line in
a.txt
rather than partial matches. – Paused until further notice. – 2010-12-13T21:18:11.060