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
compare files danabo Shell Programming and Scripting 3 05-19-2008 12:09 PM
ZaReason's MegaLap is a desktop replacement with an Ubuntu twist iBot UNIX and Linux RSS News 0 04-07-2008 11:00 AM
Suspending jobs (CTRL+Z) with a twist d11wtq UNIX for Dummies Questions & Answers 2 06-21-2005 03:59 AM
compare files again ithomp Shell Programming and Scripting 2 06-17-2004 03:46 AM
how do I log into this machine - with a twist... xyyz UNIX for Dummies Questions & Answers 1 08-29-2001 08:57 AM

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-20-2008
Autumn Tree Autumn Tree is offline
Registered User
  
 

Join Date: Nov 2008
Location: Atlanta
Posts: 6
Compare 2 files yet again but with a twist

Ok so I have a file which contains 2 columns/fields and I have another file with 2 columns. The files look like:

file1:
1 33
5 345
18 2
45 1
78 31

file2:
1 c1d2t0
2 c1d3t0
3 c1d4t0
4 c1d4t0
5 c2d1t0
6 c2d1t0
7 c2d1t0
8 c2d1t0
9 c2d1t0
10 c2d1t0
11 c2d5t0
18 c3d1t0
45 c5d10t0
78 c3d12t0

---

I need to get the following result:
1 33 c1d2t0
5 345 c2d1t0
18 2 c3d1t0
45 1 c5d10t0
78 31 c3d12t0

---

I know there is an elegant awk solution but I am getting a little lost when doing the search on the first column.
  #2 (permalink)  
Old 11-20-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,849
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR == FNR { 
  _[$1] = $2 
  next 
  }
$2 = $2 FS _[$1]
' file2 file1
  #3 (permalink)  
Old 11-20-2008
Autumn Tree Autumn Tree is offline
Registered User
  
 

Join Date: Nov 2008
Location: Atlanta
Posts: 6
Thank you - works perfectly!!!
  #4 (permalink)  
Old 11-20-2008
yogi_raj_143 yogi_raj_143 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 16
It seems you are dumping & combining the rows which have common first element in the row for each files
you can use join command

Quote:
join file1 file2
refer man join for more details
  #5 (permalink)  
Old 11-23-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,849
Quote:
Originally Posted by yogi_raj_143 View Post
It seems you are dumping & combining the rows which have common first element in the row for each files
you can use join command



refer man join for more details
In order to use join the input files should be sorted.
On Solaris, for example, using bash (because of the process substitution),
you can use something like this:

Code:
join -o 2.1 2.2 1.2 <(sort dkl) <(sort gdl)
  #6 (permalink)  
Old 11-21-2008
Autumn Tree Autumn Tree is offline
Registered User
  
 

Join Date: Nov 2008
Location: Atlanta
Posts: 6
yogi_raj: I think you didn't understand the question. join is not the solution. the above awk statement is... thanks for the effort!
  #7 (permalink)  
Old 11-21-2008
Autumn Tree Autumn Tree is offline
Registered User
  
 

Join Date: Nov 2008
Location: Atlanta
Posts: 6
@radoulov:

unfortunately I didn't have access to my sun box yesterday - and now when I test it I get this:

/usr/xpg4/bin/awk: syntax error Context is:
>>> NR == FNR {_[$1] = $2 next <<<

with nawk it's:
nawk: syntax error at source line 1
context is
NR == FNR {_[$1] = $2 >>> next <<< } $2 = $2 FS _[$1]
nawk: illegal statement at source line 1
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 11:58 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