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.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-28-2005
Registered User
 

Join Date: Jul 2005
Posts: 30
using awk

Hi,
I have one file like below:
0744 111305 9.92 52008
1552 111305 1.70 52008
5345 111305 0.40 52008
1552 111305 1.58 52056

and other file like below:
1552 name2
0744 name3
5345 name4

I need to paste the "name" column in the second file to the fist file if there is any match on the fist column. I can't use paste command since first file will have duplicated ID on the first column.

This is what I'd like to show on the final file:
0744 name3 11305 9.92 52008
1552 name2 111305 1.70 52008
5345 name4 111305 0.40 52008
1552 name2111305 1.58 52056

Any help would be very appreciated!
Reply With Quote
Forum Sponsor
  #2  
Old 11-28-2005
Registered User
 

Join Date: Jul 2005
Posts: 30
shell or loop?

Hi,
I am new to shell and awk.
Can anyone give me a hint where I should start with it?

Thanks!
Reply With Quote
  #3  
Old 11-28-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
nawk -f cin.awk otherFile oneFile

with cin.awk being:
Code:
NR==FNR { arr[$1]=$2; next }
$1 in arr { $1 = $1 OFS arr[$1] }
1
Reply With Quote
  #4  
Old 11-28-2005
Registered User
 

Join Date: Jul 2005
Posts: 30
nawk NR==FNR

Hi,
Thanks a lot! It worked.
I am wondering if you could explain with plain English because I like to know how I can modify the code for my program.



Thanks again!
Reply With Quote
  #5  
Old 11-28-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
here's the code with embedded comments:

Quote:
Originally Posted by vgersh99
nawk -f cin.awk otherFile oneFile

with cin.awk being:
Code:
# 'NR == FNR' results in 'true' ONLY for the FIRST file passed to script
# do 'man nawk' to get a detailed description(s) on FNR and NR
# when 'FNR==NR' create an array 'arr' indexed by the value of FIRST field '$1'
# and assigned a value of the SECOND field '$2'. 'next' skips the rest of the
# script and goes to the next record/line
NR==FNR { arr[$1]=$2; next }


# we get here for the file(s) OTHER than the  FIRST file
# If the value of the FIRST field '$1' is in the array '$1' - execute the action
# within the curlies: suffix the value of the FIRST field '$1' with the value of
# OFS [OutputFieldSeparator] followed by the value found in the array 'arr'
# indexed by the value of the FIRST field '$1' 
$1 in arr { $1 = $1 OFS arr[$1] }


# this is a shorthand for '{print}' which prints the current record
1

Last edited by vgersh99; 11-28-2005 at 09:26 PM.
Reply With Quote
  #6  
Old 11-28-2005
Registered User
 

Join Date: Sep 2005
Location: Chennai
Posts: 80
There is command in UNIX which does a "join" on files using their columns (analogous to database table joins). The only condition being the fields on which join to be performed should be sorted.

Lets call the first file "file1" and the second file "file2".
Heres what you can do:

sort file1>sfile1;sort file2>sfile2;
join -a1 -o 1.1,1.2,1.3,1.4,2.2 sfile1 sfile2

Explanation:
-o lets you specify which columns you want to see in ur output. Columns belonging to the first file are denoted by 1 followed by a decimal point and the column number. Columns belonging to the second file are denoted by 2 followed by a decimal point and the column number. So here we trying to see all columns from the first file and the second column from the second file in the order specified.

The -a flag prints unpaired lines from file1.
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 06:03 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