Need to search string from file 1 in one of columns of file 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to search string from file 1 in one of columns of file 2
# 1  
Old 12-10-2012
Need to search string from file 1 in one of columns of file 2

hello all.
I would like to kindly ask you, if you could help me out with one problem.
I have two files -> for ex. file1 and file2
file1 contains only various numbers
file 2 contains multiple columns with various values

What i need is to take every single number, one by one, from file 1 and check if this number occurs in column 5 of file 2 - doesnt matter if it occurs for several times

file 1
Code:
49000186672011                                                                                     
49000186682011                                                                                     
49000186692011                                                                                     
49000186702011                                                                                     
49000186712011                                                                                     
49000186722011                                                                                     
49000186732011                                                                                     
49000186742011
49000186762011

file 2
Code:
4900017287 2011 WA CO11N 49000186672011
4900017288 2011 WA CO11N 49000186682011
4900017289 2011 WA CO11N 49000186692011
4900017290 2011 WA CO11N 49000186702011
4900017291 2011 WA CO11N 49000186712011
4900017292 2011 WA CO11N 49000186722011
4900017293 2011 WA CO11N 49000186732011
4900017294 2011 WA CO11N 49000186742011
4900017296 2011 WA CO11N 49000186762011

Desired output if matching :

Code:
4900017287 2011 WA CO11N 49000186672011  X
4900017288 2011 WA CO11N 49000186682011  X
4900017289 2011 WA CO11N 49000186692011  X
4900017290 2011 WA CO11N 49000186702011  X
4900017291 2011 WA CO11N 49000186712011  X
4900017292 2011 WA CO11N 49000186722011  X
4900017293 2011 WA CO11N 49000186732011  X
4900017294 2011 WA CO11N 49000186742011  X
4900017296 2011 WA CO11N 49000186762011  X

Thank you all in advance for any useful hints.

I tried already some loops, awk searches but without any desired success.

Last edited by Scrutinizer; 12-10-2012 at 04:48 PM.. Reason: code tags
# 2  
Old 12-10-2012
try:
Code:
awk 'NR==FNR {a[$1]=$1;next} a[$5] {print $0," X"}' file1 file2

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 12-10-2012
thanks ! seems to be working well!
# 4  
Old 12-10-2012
Code:
awk 'NR==FNR {a[$1];next} $5 in a {print $0,"X"}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Search a string in a file which is also present in another file in UNIX

Hi there, I am new to Unix and had below requirement to finish my task. I have file1.dat which has data as shown below. case1.txt case2.txt case3.txt case4.txt file1.dat has only file names I have folder which has above files mentioned in file1.dat ./all_files case1.txt... (6 Replies)
Discussion started by: raj028
6 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

10. Shell Programming and Scripting

how to search string and number in one file and check in the other file

Hi, Can anyone help in the below problem. file1 has the below contents fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist fileset 999 primary-ilist inode 115016 dup block ->... (9 Replies)
Discussion started by: knshree
9 Replies
Login or Register to Ask a Question