The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to compare 2 files. ashoka123 Shell Programming and Scripting 6 10-21-2008 02:38 AM
need to compare two files sriu76 UNIX for Dummies Questions & Answers 1 09-25-2008 05:57 AM
compare files danabo Shell Programming and Scripting 3 05-19-2008 12:09 PM
compare files spt Shell Programming and Scripting 1 05-09-2008 10:57 AM
help! need to compare files danielsf Shell Programming and Scripting 6 07-09-2003 05:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-16-2009
borderblaster borderblaster is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
awk NR==FNR compare 2 files produce a 3rd

hi, i have two files, both with 3 columns, the 3rd column has common values between the two files and i want to produce a 3rd file with 4 columns.

file 1

a, ,b c

file 2

a, b ,d

I want to compare the 3rd value and if a match print to file 3 with the 3 columns from the first file plus the entitlement column from file 2 added as 4th column , delimited.

file 3

a, b, c, d

I have been trying things like this borrowed off other posts but its outputing completely wrong, ive never really used awk that much

awk 'NR==FNR{x[$3]=$2;next}$3 in x&&$2=x[$3]' OFS="," FS="," file2 file1

Last edited by borderblaster; 03-18-2009 at 09:04 AM..
  #2 (permalink)  
Old 03-16-2009
pmm pmm is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 50
Code:
nawk -F, '
{ if ( FILENAME == "file1" )
        _[$3]=$0
        else
        _[$3]=_[$3]","$1 }
END { for ( i in _ ) print _[i] }
' file1 file2
  #3 (permalink)  
Old 03-17-2009
borderblaster borderblaster is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
When I run this I get a syntax error, I only have awk on the server as well

awk -F, '{ if ( FILENAME == "file1.txt" )_[$3]=$0 else _[$3]=_[$3]","$1 }END { for ( i in _ ) print _[i] }' file1.txt file2.txt
awk: cmd. line:1: { if ( FILENAME == "file.txt" )_[$3]=$0 else _[$3]=_[$3]","$1 }END { for ( i in _ ) print _[i] }
awk: cmd. line:1: ^ syntax error
  #4 (permalink)  
Old 03-17-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk -F, 'NR==FNR{a[$3]=$1;next}$3 in a {$4=a[$3]}1' OFS="," file2 file1
Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.

Regards
  #5 (permalink)  
Old 03-17-2009
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
Code:
$ cat a.txt
cust1, ac1, 100
cust2, ac2, 200
$ cat b.txt
100000, cust1, 100
2000000, cust2, 200
$ join -a 1 -a 2 -1 3 -2 3 -t"," -o 1.1 1.2 1.3 2.1 a.txt b.txt
cust1, ac1, 100,100000
cust2, ac2, 200,2000000
Mind you, both files need to be sorted on the key column (stbid). In this case a.txt and b.txt are already sorted.

Last edited by rikxik; 03-17-2009 at 06:00 AM.. Reason: sort requirement
  #6 (permalink)  
Old 03-17-2009
borderblaster borderblaster is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
franklin, ive realised i need to compare two columns, i.e. if both customer id and account id match in both files then append packageid to the end of the relevent line in the first file. Can that be done?
  #7 (permalink)  
Old 03-17-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Post an example of both files. You have not specify a colomn for the account id in your second file.

Regards
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 02:04 PM.


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