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 and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2005
cin2000 cin2000 is offline
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!
  #2 (permalink)  
Old 11-28-2005
cin2000 cin2000 is offline
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!
  #3 (permalink)  
Old 11-28-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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

  #4 (permalink)  
Old 11-28-2005
cin2000 cin2000 is offline
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!
  #5 (permalink)  
Old 11-28-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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-29-2005 at 12:26 AM..
  #6 (permalink)  
Old 11-29-2005
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0