Search a file for patterns from another file to another with awk | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Search a file for patterns from another file to another with awk

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-11-2012
beca123456 beca123456 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 20 June 2014, 9:51 PM EDT
Posts: 70
Thanks: 36
Thanked 0 Times in 0 Posts
Search a file for patterns from another file to another with awk

Hi all !

I have 2 files:
file1:

Code:
1|AAA|123456
2|BBB|098765432
...

file2:

Code:
-|klk|AAA|$123.00|Qty.2|US
-|opi|EEE|$23.00|Qty.4|US
...

Output:

Code:
1|AAA|-|klk|AAA|$123.00|Qty.2|US


I would need to search the 3rd field of file2 for the patterns contained in the 2nd field of file1.
And then, for the matching lines, return fields 1 and 2 from file1 followed by the entire line of file2.

I tried several variants of the code below but don't know how to define the different fields from different files:

Code:
awk 'BEGIN{FS=OFS="|"}{NR==FNR}{a[$2]=++i;next} { if ( $3 in a) {print $0}}' file1 file2

Any help would be great, thanks !
Sponsored Links
    #2  
Old 12-11-2012
Joseph_TKLee Joseph_TKLee is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 24 September 2013, 6:55 AM EDT
Location: Brisbane, Australia
Posts: 40
Thanks: 3
Thanked 10 Times in 10 Posts

Code:
for V_STR in `cat file.1`; do
        V_F1=`echo $V_STR | awk -F\| '{print $1}'`
        V_F2=`echo $V_STR | awk -F\| '{print $2}'`
        V_File=`grep $V_F2 file.2` && echo $V_F1"|"$V_F2"|"$V_File
done

Cheers,
Sponsored Links
    #3  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
small correction to your code...


Code:
$ awk 'BEGIN{FS=OFS="|"}
  NR==FNR{a[$2]=$1 FS $2;next} {if ($3 in a) {print a[$3],$0}}' file1 file2

1|AAA|-|klk|AAA|$123.00|Qty.2|US


Last edited by pamu; 12-11-2012 at 01:53 AM..
The Following User Says Thank You to pamu For This Useful Post:
beca123456 (12-11-2012)
    #4  
Old 12-11-2012
beca123456 beca123456 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 20 June 2014, 9:51 PM EDT
Posts: 70
Thanks: 36
Thanked 0 Times in 0 Posts
Thanks a lot pamu, it works very well !
Sponsored Links
    #5  
Old 12-11-2012
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 16 January 2014, 5:23 AM EST
Posts: 71
Thanks: 16
Thanked 11 Times in 11 Posts
Pamu,

can you please explain your code ?

Thank You
Sponsored Links
    #6  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by Fundix View Post
can you please explain your code ?

Code:
awk 'BEGIN{FS=OFS="|"}               # Set FS=OFS="|"

NR==FNR{a[$2]=$1 FS $2;next}         # Reading file1. Create array a and assigns value to a[$2]=$1 FS $2, means a[$2]=$1"|"$2, And next for slipping next line of code.
  
{if ($3 in a) {print a[$3],$0}}'     # Reading file2. Check is $3 present in array a(is $3 present in index of array a) then print a[$3](means $1"|"$2 from file1) and $0(from file2)

I hope this helps

pamu
The Following User Says Thank You to pamu For This Useful Post:
Fundix (12-11-2012)
Sponsored Links
    #7  
Old 12-11-2012
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 16 January 2014, 5:23 AM EST
Posts: 71
Thanks: 16
Thanked 11 Times in 11 Posts
I was wondering about
Code:
NR==FNR

and have found that it's a trick to be sure we are reading file1.
To be sure you are reading file2 don't you need to use
Code:
NR!=FNR

?

How can we manage it in case of we need to read more than 2 files ?

Thank You
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
reading lines from a file between two search patterns davidtd Shell Programming and Scripting 3 10-17-2011 01:48 PM
to read two files, search for patterns and store the output in third file vaibhavkorde Shell Programming and Scripting 3 04-14-2011 03:48 AM
Need help in retrieving log from a UNIX file using the search patterns msrayudu Shell Programming and Scripting 5 01-19-2011 11:10 PM
Perl - How to search a text file with multiple patterns? Sp3ck Shell Programming and Scripting 2 03-25-2009 11:30 PM
How to parameterize multiple search patterns and generate a new file augustinep UNIX for Dummies Questions & Answers 6 07-30-2003 08:50 AM



All times are GMT -4. The time now is 01:21 AM.