The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
One doubt namishtiwari Shell Programming and Scripting 2 06-29-2008 11:23 PM
doubt in awk jisha Shell Programming and Scripting 4 04-16-2008 01:02 AM
AWK doubt Rafael.Buria Shell Programming and Scripting 2 02-06-2008 08:00 AM
doubt in sed matrixmadhan UNIX for Dummies Questions & Answers 3 08-03-2005 08:34 AM
doubt it sed esham Shell Programming and Scripting 2 03-07-2005 10:52 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-10-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 135
Arrow Doubt in awk

Hi All,

I have two files as given below:

fileA
1234|aaaaa|vvvv
2222|bbbbbb|cbxxbjh
3333|cccc|jhjhj

fileB
3434|bbbcc|cbxxbjh
1234|cat|bullet
3333|cccc|jhjhj

I need a script that reads the first column from fileA (i.e 1234) and searches in fileB in the first parameter(i.e first column), if match found ( for example 1234 - line1 in fileA and 1234 - line 2 in fileB), then append the rest of the row in file and write to a new file

Example
Get first parameter in first row in fileA = 1234
search for the line with first parameter as 1234 in fileB.We get line 2.
write 1234|aaaaa|vvvv|cat|bullet to fileC.

Thanks in advance
JS
Reply With Quote
Forum Sponsor
  #2  
Old 10-10-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 727
Post what have you tried so far and where exactly are you stuck?
Reply With Quote
  #3  
Old 10-10-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 135
using awk i can get the first parameter from each line in fileA.
But piping it to fileB for search is where am really stuck
Reply With Quote
  #4  
Old 10-10-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 727
Search the forum for NR==FNR
Reply With Quote
  #5  
Old 10-10-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 135
I have been trying with this command
awk 'NR==FNR{_[NR]=$0;next;}{$1=_[FNR];print}' file1 file2

but i donot know how exactly this works. And it is printing only fileA rows. i don know where to tune it.
Reply With Quote
  #6  
Old 10-10-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 135
I have searched the forum, still i dont understand where to make the necessary change to meet my need.

I have got n number of files to do the processig in this way.
Can someone plese help
Reply With Quote
  #7  
Old 10-10-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,204
Please don't bump up questions, read the rules.
Try this:

Code:
awk -F"|" 'NR==FNR{a[$1]=$2"|"$3;next}
a[$1]{print $0"|"a[$1];next}1' fileB fileA
Regards
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0