awk or grep to search one column and output the other | 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.

awk or grep to search one column and output the other

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-20-2013
Manyaka Manyaka is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 July 2013, 4:29 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
awk or grep to search one column and output the other

Hello,

it would be great if someone can help me with the following:
I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched?


Code:
cat fileA | while read row ; do
grep "$row" fileB | awk -F"\t" '{print $2}' > fileC


Last edited by Scott; 07-20-2013 at 03:47 AM.. Reason: Code tags
Sponsored Links
    #2  
Old 07-20-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 30 July 2014, 5:25 PM EDT
Location: Zürich
Posts: 8,444
Thanks: 328
Thanked 1,031 Times in 902 Posts
This seems to have been asked (in one guise or another) a million times.


Code:
$ awk 'NR==FNR{A[$0]=1; next} A[$1] {print $2}' fileA fileB > fileC

The horizontal tab (\t) is already a default field separator, so unless you plan to explicitly count fields using it there's no need to specify it with -F.
Sponsored Links
    #3  
Old 07-20-2013
Manyaka Manyaka is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 July 2013, 4:29 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the fast reply. I now have:


Code:
cat fileA | while read row ; do
$ awk 'NR==FNR{A[$1]=1; next} A[$1] {print $2}' fileA fileB > fileC

The "$" seems to produce an error. What does the "A" stand for? And why "=1"? Shouldnt it be "=row"?

Sorry, I am very new to Shell Programming..

---------- Post updated at 03:34 AM ---------- Previous update was at 03:28 AM ----------

I need the "do" command because each search produces another output-file which is processed after..

Last edited by Scott; 07-20-2013 at 04:32 AM.. Reason: Code tags
    #4  
Old 07-20-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 30 July 2014, 5:25 PM EDT
Location: Zürich
Posts: 8,444
Thanks: 328
Thanked 1,031 Times in 902 Posts
$ signifies a command prompt, it's not part of the code.

You don't need cat ... | while.

Just

Code:
awk 'NR==FNR{A[$0]=1; next} A[$1] {print $2}' fileA fileB > fileC

A is just a variable, call it what you want:

Code:
awk 'NR==FNR{row[$0]=1; next} row[$1] {print $2}' fileA fileB > fileC

=1 is just to record that the value of the record ($0) from the first file (FR==FNR) has been stored. This is used later (A[$1]) to test against the first field of the second file, in which case $2 from that file is displayed.
Sponsored Links
    #5  
Old 07-20-2013
Manyaka Manyaka is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 July 2013, 4:29 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Scott can I send you an eMail? I tried but " o be able to send PMs your post count must be 10 or greater."

---------- Post updated at 04:47 AM ---------- Previous update was at 04:24 AM ----------

Would like to send you the data-file and the script to figure it out whats wrong..
Sponsored Links
    #6  
Old 07-20-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 30 July 2014, 5:25 PM EDT
Location: Zürich
Posts: 8,444
Thanks: 328
Thanked 1,031 Times in 902 Posts
It would be better if you posted the script, and a representative sample of the data file here, obfuscating any sensitive data before doing so.
The Following User Says Thank You to Scott For This Useful Post:
Manyaka (07-20-2013)
Sponsored Links
    #7  
Old 07-20-2013
Manyaka Manyaka is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 20 July 2013, 4:29 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Scott, thanks again for your help! I finally figured it out - found a very simple solution: I just had to add an ^ before "$row" to only search within the first column..


Code:
cat fileB | while read row ; do grep ^"$row" fileA | awk -F"\t" '{print $2}' > fileC


Last edited by Scott; 07-20-2013 at 04:31 PM.. Reason: Code tags
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
awk strings search + print next column after match sdf Shell Programming and Scripting 10 03-30-2012 08:09 PM
To search a word in particular column using awk praveenhegde Shell Programming and Scripting 2 07-18-2011 04:20 AM
awk search column, print line Matthias03 Shell Programming and Scripting 2 12-04-2009 09:03 PM
AWK in column search imipsita.rath Shell Programming and Scripting 3 09-04-2009 02:21 PM
Search for a column by name in awk kskkarthik Shell Programming and Scripting 2 10-24-2008 11:33 PM



All times are GMT -4. The time now is 07:15 PM.