The following code works, but is quite slow:
#!/bin/bash
for f in *.xml; do
for r in $(cat ../reports.txt); do
grep -q "$r" $f
if [ $? -eq 0 ]; then
echo "$r,$f"
fi
done
done
This could be sped up using:
for f in *.xml; do
grep -lif ../reports.txt $f
done
However, this does not display the lines in reports.txt that were found to match the file ($f).
How would you display the matching lines from reports.txt that were found in each file along with the name of the file that had 1 or more matches from reports.txt?
For example, if reports.txt contains:
aardvark
aardwolf
anteater
And one of the files (e.g., story.txt) contains:
This is a story about an aardvark and an aardwolf.
Then I’m trying to produce the following output:
story.txt,aardvark
story.txt,aardwolf
If it helps, the lines in reports.txt aren’t regular expressions: they are plain text strings.
Any ideas?
take a look this, does it fit your requirement?
test was done with grep: