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
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 08:27 AM
joining the data vakharia Mahesh UNIX Desktop for Dummies Questions & Answers 0 03-15-2007 08:49 AM
Help with joining two files rjlohman Shell Programming and Scripting 3 09-27-2006 08:55 AM
joining files Manu UNIX for Dummies Questions & Answers 2 04-25-2005 09:24 AM
joining 2 files webtekie UNIX for Dummies Questions & Answers 1 10-21-2003 07:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 12-30-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

Hi,

Awk should be ok for you.

a:
Code:
20071121|12|Mohit|DAV
20071222|13|Dev|MRSD
20071011|14|Rajan|ST
20071011|15|Lali|OTH
b:
Code:
12|KKP|Pun|Mohit
14|FDK|Pas|Abhi
15|SAD|Hp|Lali
out:
Code:
12|Mohit|20071121|DAV|KKP|Pun
15|Lali|20071011|OTH|SAD|Hp
code:
Code:
nawk 'BEGIN{FS="|"}
NR==FNR {
temp1=sprintf("%s|%s",$2,$3)
t[temp1]=sprintf("%s|%s|%s|%s",$2,$3,$1,$4)}

NR!=FNR {
temp2=sprintf("%s|%s",$1,$4)
if (t[temp2])
printf("%s|%s|%s\n",t[temp2],$2,$3)
}
' a b
Reply With Quote
Forum Sponsor
  #9  
Old 01-02-2008
Registered User
 

Join Date: Dec 2007
Posts: 16
how to use "where" condition?

if i want to get the row in which a particular condition is req,
than how can i do it?
Like in the above given file if i want to extract only the row where second column =12 in the first file.
Please help me as soon as possible.
Thanks
Reply With Quote
  #10  
Old 01-04-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Code:
column_f1=2
value=12

awk 'FNR == NR && $'${column_f1}' == '${value}' { n=FR;print;break }
/'${value}'/ FNR == n {print}' FS=\| f1 f2
20071121|12|Mohit|DAV
Anyway , you can achieve your goal using join.
Reply With Quote
  #11  
Old 06-17-2008
Registered User
 

Join Date: May 2008
Posts: 6
Could you please explain the code?

Hi,

I checked your code and it works great, but I can understand it very well...


nawk 'BEGIN{FS="|"}
NR==FNR { <<< what does this section do? I thought it meant when the combination of fields is found then do whatever.... but the print is in the next section
temp1=sprintf("%s|%s",$2,$3)
t[temp1]=sprintf("%s|%s|%s|%s",$2,$3,$1,$4)}

NR!=FNR {
temp2=sprintf("%s|%s",$1,$4)
if (t[temp2])
printf("%s|%s|%s\n",t[temp2],$2,$3)
}
' a b

I'm changing some values to try to understand it better but I'm still confused...

Thanks,

Quote:
Originally Posted by summer_cherry View Post
Hi,

Awk should be ok for you.

a:
Code:
20071121|12|Mohit|DAV
20071222|13|Dev|MRSD
20071011|14|Rajan|ST
20071011|15|Lali|OTH
b:
Code:
12|KKP|Pun|Mohit
14|FDK|Pas|Abhi
15|SAD|Hp|Lali
out:
Code:
12|Mohit|20071121|DAV|KKP|Pun
15|Lali|20071011|OTH|SAD|Hp
code:
Code:
nawk 'BEGIN{FS="|"}
NR==FNR {
temp1=sprintf("%s|%s",$2,$3)
t[temp1]=sprintf("%s|%s|%s|%s",$2,$3,$1,$4)}

NR!=FNR {
temp2=sprintf("%s|%s",$1,$4)
if (t[temp2])
printf("%s|%s|%s\n",t[temp2],$2,$3)
}
' a b
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:16 AM.


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