Take a list if strings from a file and search them in a list of files and report them

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take a list if strings from a file and search them in a list of files and report them
# 1  
Old 07-29-2011
Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents.
-----cat 1.txt-----

I have 3 more files in a folder
-----ls -lrt-------

The contents of those three files are similar format with different data values (All the three files are tab delimited)
-----cat A1.txt----
A   X   1234    B   1234
A   X   5678    B   1234
A   X   1256    B   1256

-----cat A2.txt----
A   Y   8888    B   1234
A   Y   9999    B   1256
A   X   1234    B   1256

-----cat A3.txt----
A   Y   6798    C   1256

My objective is to do a search on all the A1,A2 and A3 (Only for the 3rd column of the TAB delimited file)for text in 1.txt
and the output must be redirected to the file matches.txt as given below.
/home/A1.txt:A   X   1234    B   1234
/home/A1.txt:A   X   5678    B   1234
/home/A1.txt:A   X   1256    B   1256
/home/A2.txt:A   X   1234    B   1256

Could someone please help with this?

Last edited by radoulov; 07-31-2011 at 05:34 PM.. Reason: Code tags!
# 2  
Old 07-29-2011
awk 'NR==FNR{a[$0]=1}$3 in a{print FILENAME":"$0}' 1.txt A*

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-29-2011
Could you please explain how this works? Also I need to redirect the output to a file.
# 4  
Old 07-29-2011
It is storing the contents of 1.txt into array "a", then it is checking if third column on the other files is present in that array. If so, it is printing the filename along with the line that the match occurred. To redirect the output use:
awk 'NR==FNR{a[$0]=1}$3 in a{print FILENAME":"$0}' 1.txt A* > matches.txt

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-29-2011
The same example works fineSmilie. But when I apply the same to a real time scenario. It is not working.Smilie.
awk 'NR==FNR{a[$0]=1}$19 in a{print FILENAME":"$0}' 2.txt $RESPONSE_DIR/DOWNLOAD*.dat > 2matches.txt

I try to get a list of files from RESPONSE_DIR, the position is 19.
19th position is the last position in the file. Also there may be spaces with in the values ...

---------- Post updated at 11:56 AM ---------- Previous update was at 07:26 AM ----------

Also this is not working for more than 200 records. Smilie

Last edited by radoulov; 07-31-2011 at 05:35 PM.. Reason: Code tags.
# 6  
Old 07-30-2011
Files are very similar i.e both are in Linux mode. Urgent situation. Please h
# 7  
Old 07-30-2011
quite simple: use the "grep" utility with the "-f" (file) option. See "man grep" for a detailed explanation:

grep -f /file/with/key/values /path/to/A[123].txt > match.txt

I hope this helps.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. UNIX for Beginners Questions & Answers

Using find to output list of files with specific strings

This is my problem, I am using the following code to extract the file names with specific strings 0.01: find ./ -name "*.txt" -exec grep -H '0.01' {} + It works wonders with a small sample. However, when I use it in a real scenario it produces an empty file -even though I am sure there are... (11 Replies)
Discussion started by: Xterra
11 Replies

3. UNIX for Dummies Questions & Answers

Search list of files

Hi All, I Have 2 files. file1: abc.xml.gz abcd.xml.gz xyz.xml.gz X.xml.gz File2 abc. x. cde. unix. My requirment is search File1 using File2 and need below ouput. (6 Replies)
Discussion started by: j.vutakanti
6 Replies

4. Shell Programming and Scripting

Searching for a list of strings in a file with Python

Hi guys, I'm trying to search for several strings, which I have in a .txt file line by line, on another file. So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt. So far, I've been able to do this, to search for individual strings: ... (1 Reply)
Discussion started by: starriol
1 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. UNIX for Dummies Questions & Answers

Find Multiple Strings from a list of *.gz files withour decompressing...

Hello Team, There is this situation where there are around 20 *.gz files and i want to search multiple words from all those files. Example as below : filea.gz fileb.gz filec.gz now i want to search words "hi" and "hello" from all these 3 files without... (4 Replies)
Discussion started by: varun87
4 Replies

7. Shell Programming and Scripting

Awk search for a element in the list of strings

Hi, how do I match a particular element in a list and replace it with blank? awk 'sub///' $FILE list="AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA,... (2 Replies)
Discussion started by: grossgermany
2 Replies

8. Shell Programming and Scripting

Want unix script to search a list of strings

Want unix script to search a list of strings (stored line by line in a file), search all the matching lines of files under directories and subdirectories in different unix machines(not one box diff boxes - all the unix box names and username/password are stored with a comma delimitor in flat file)... (2 Replies)
Discussion started by: adminuser
2 Replies

9. Shell Programming and Scripting

list and export strings in a file

Hi all, I have a file looks something like this: ~~~~~~~~~~~~~~~~~~~~~~~~~ aaaa bbbbbb ccc dddddddd ~~~~~~~~~~~~~~~~~~~~~~~~~ I will like a script that can echo line1 export line1 as env variable echo line2 export line2 as env variable etc etc Thanks in advance (3 Replies)
Discussion started by: Avatar Gixxer
3 Replies

10. UNIX for Advanced & Expert Users

Search a list of lines in file into files

I have a file (file1) which is like host1 host2 host3 host4 the list goes on............ Now I want the above lines in files to be compared with files under /opt/new/ File names are as below: Dev Prod QA And suppose host1 from file1 is found under Dev(file under /opt/new)... (2 Replies)
Discussion started by: sriram003
2 Replies
Login or Register to Ask a Question