File find | xargs grep for pattern file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File find | xargs grep for pattern file
# 1  
Old 09-10-2010
File find | xargs grep for pattern file

Folks

I've been struggling this with for far too liong now and need your help!

I've been happily using grep for a search of a directory, to list the files which contain a string:

Code:
find . -type f -mtime -5 -print | xargs grep -l 'invoiceID=\"12345\"'

Now the list of 'invoiceID' I am looking for is getting huge so I was hoping to list them all in a pattern list.

The string for the files I'm searching through are always in the format invoiceID="12345"

I've created a file Pattern.txt

Code:
> cat Pattern.txt
'invoiceID=\"12345\"'
'invoiceID=\"23456\"'
'invoiceID=\"34567\"'

...and three corresponding files

Code:
> cat Files*
Value"12" invoiceID="12345" Number"45"
Value"23" invoiceID="23456" Number"56"
Value"34" invoiceID="34567" Number"67"

When I run the following, it doesn't find anything:

Code:
find . -type f -mtime -5 -print | xargs grep -l -f Pattern.txt

I've tried numerous other formats, without success.

Any ideas ?!!??

Dave.

Last edited by Scott; 09-10-2010 at 01:23 PM.. Reason: Added more code tags
# 2  
Old 09-10-2010
change your Pattern.txt to:
Code:
invoiceID="12345"
invoiceID="23456"
invoiceID="34567"

Basicly saing, when using pattern file with grep, escaping and quoting isn't necessary.
# 3  
Old 09-13-2010
Perfect - that works a treat!
# 4  
Old 09-17-2010
Is it possible to display alongside the filename, the entry within the Pattern.txt file it's has matched against ?
# 5  
Old 09-18-2010
Quote:
Originally Posted by daveaasmith
Is it possible to display alongside the filename, the entry within the Pattern.txt file it's has matched against ?
If you have gnu grep you can use -H instead of -l (might exists on other versions also), which will print the line that matched with the name of file on every line. At least on the AIX I use at work there are all the usual gnu tools, just prefixed with g, eg. ggrep for gnu grep.

However, -H doesn't do exactly what you asked for, since it prints the line from the file that matched instead of the matching pattern from Pattern.txt. However simple patterns like invoiceID='xxxx' are easy to identify from among (hopefully) formatted xml. And I'm just guessing it's xml.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

2. Shell Programming and Scripting

Xargs or Find with output to a file

Hi, I've got to setup a script that will run daily, and find a log file of a certain age, and then compress and transfer this file to a new location. so far i've been able to specify the file i want with: find . -name 'filename.*.log' -mtime 14 -exec compress -vf {} \; this prints out... (4 Replies)
Discussion started by: horhif
4 Replies

3. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

4. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

5. Shell Programming and Scripting

Find common terms in two text file, xargs, grep

Hello, I'm interested in finding all occurrences of the terms in file1 in file2, which are both csv files. I can do this with a loop but I'm interested in knowing if I can also do it with the help of xargs and grep. What I have tried: cat file1 | xargs grep file2 The problem is that... (15 Replies)
Discussion started by: eon
15 Replies

6. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

7. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. UNIX for Dummies Questions & Answers

Grep in a file for a particular pattern in a particular position witihn the file

Assume I have a file with a lot of data sets like 123 abc 01 456 def 02 789 ghi and I only want to grep all that datasets from my file having the pattern '02' at the postion 9-10 to get only 456 def 02 So I could group the datsets into three files according to the position 9-10, one... (9 Replies)
Discussion started by: ABE2202
9 Replies
Login or Register to Ask a Question