Grep from multiple patterns multiple file multiple output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep from multiple patterns multiple file multiple output
# 1  
Old 10-31-2013
Grep from multiple patterns multiple file multiple output

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,
# 2  
Old 10-31-2013
I guess/hope you have
Code:
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 patternfileis easier and avoids duplicates (in case multiple patterns match)
Code:
grep -f otherdir/patterns.txt *.txt > otherdir/output.txt

For separate output files have a loop:
Code:
for file in *.txt
do
  grep -f otherdir/patterns.txt "$file" > otherdir/output."$file"
done

# 3  
Old 11-01-2013
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.

Code:
for file in *.count.txt
do
  xargs -I {} grep  "$file" < strings.txt > output."$file"
done

# 4  
Old 11-02-2013
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

How to use grep with multiple patterns?

I am trying to grep a variable with multiple lines with multiple patterns below is the pattern list in a variable called "grouplst", each pattern is speerated by "|" grouplst="example1|example2|example3|example4|example5|example6|example7" I need to use the patterns above to grep a... (2 Replies)
Discussion started by: ajetangay
2 Replies

3. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

4. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

5. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

6. Shell Programming and Scripting

grep for multiple patterns

I have a file with many rows. I want to grep for multiple patterns from the file. For eg: XX=123|YY=222|ZZ=566 AA=123|EE=222|GG=566 FF=123|RR=222|GG=566 DD=123|RR=222|GG=566 I want the lines which has both XX and ZZ. I know I can get it like this. grep XX file | grep YY But... (10 Replies)
Discussion started by: tene
10 Replies

7. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies

8. Shell Programming and Scripting

Grep for Multiple patterns

Hi All, I have a file. I need to find multiple patterns in a row and need those rows to divert to new file. I tried using grep -e / -E / -F options as given in man. But its not working. ==> cat testgrep.txt william,fernandes,xxxxx mark,morsov,yyyy yy=,xx= yyyy=,xxxx== ==>... (7 Replies)
Discussion started by: WillImm123
7 Replies

9. Shell Programming and Scripting

Grep multiple patterns

Hi, Can we grep multiple patterns in UNIX. for example: cat /x/y/oratab | grep -i "pattern1|pattern2" .... etc I require the syntax for multiple patterns. | is not working as I explained in example. Malay (4 Replies)
Discussion started by: malaymaru
4 Replies

10. UNIX for Dummies Questions & Answers

grep for multiple patterns

I want to get a list of all the files in the current directory that have two patterns. I can do first grep of one pattern and then with the output do the grep of the second pattern. if the output of 1st pattern search results in many files, it is very difficult to do a grep of the 2nd pattern for... (1 Reply)
Discussion started by: tselvanin
1 Replies
Login or Register to Ask a Question