Hi,
I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command.
Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns.
Xargs -I {} grep *.txt < patterns.txt >output.txt
I want to have 108 output files for the 108 input files with the 390 search pattern. How can I acheive it?
Thanks,
I guess/hope you have
xargs -I {} grep {} *.txt <otherdir/patterns.txt >otherdir/output.txt
otherwise your search in *.txt includes patterns.txt and output.txt - with random results.
The following grep -f patternfile
is easier and avoids duplicates (in case multiple patterns match)
grep -f otherdir/patterns.txt *.txt > otherdir/output.txt
For separate output files have a loop:
for file in *.txt
do
grep -f otherdir/patterns.txt "$file" > otherdir/output."$file"
done
Thanks,
But grep -f is pulling the whole file out and writing to output instead of just the matching one's. So I used the xargs. but xargs does not give me any output. Below is the code that I used.
for file in *.count.txt
do
xargs -I {} grep "$file" < strings.txt > output."$file"
done
Can you explain?
In the loop grep -f
prints exactly the same matching patterns (or less if there are duplicate output lines).
--
And why don't you have {}
after the grep
?