Need to search string from file 1 in one of columns of file 2 | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-10-2012
wiety wiety is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 4:51 AM EDT
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
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 03:48 PM.. Reason: code tags
Sponsored Links
    #2  
Old 12-10-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try:

Code:
awk 'NR==FNR {a[$1]=$1;next} a[$5] {print $0," X"}' file1 file2

The Following User Says Thank You to rdrtx1 For This Useful Post:
wiety (12-10-2012)
Sponsored Links
    #3  
Old 12-10-2012
wiety wiety is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 4:51 AM EDT
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
thanks ! seems to be working well!
    #4  
Old 12-10-2012
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 22 March 2014, 12:27 PM EDT
Posts: 2,759
Thanks: 44
Thanked 418 Times in 406 Posts

Code:
awk 'NR==FNR {a[$1];next} $5 in a {print $0,"X"}' file1 file2

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search based on 1,2,4,5 columns and remove duplicates in the same file. onesuri Shell Programming and Scripting 2 10-25-2010 05:00 AM
search string in a file and retrieve 10 lines including string line sukrish Shell Programming and Scripting 8 08-19-2010 03:53 PM
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 07:23 AM
how to search string and number in one file and check in the other file knshree Shell Programming and Scripting 9 08-24-2007 04:29 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM



All times are GMT -4. The time now is 03:24 AM.