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
selecting only few lines from many based on a common pattern damansingh Shell Programming and Scripting 2 05-28-2008 06:29 AM
Common uses for Unix rcengland UNIX for Dummies Questions & Answers 4 04-11-2008 06:37 AM
Merge files based on key sbasetty Shell Programming and Scripting 3 02-02-2007 06:05 PM
is this a common problem in X? 3bumbs plumming UNIX for Dummies Questions & Answers 3 12-06-2003 07:09 PM

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 03-16-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
merge based on common, awk help

All,

Code:
$ cat x.txt
z 11 az
x 12 ax
y 13 ay

$ cat y.txt
ay TT
ax NN

Output required:
y 13 ay TT
x 12 ax NN
  #2 (permalink)  
Old 03-16-2008
yunccll yunccll is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
Code:
>cat x.txt
z 11 az
x 12 ax
y 13 ay
>cat y.txt
ay TT
ax NN
Code:
>sort x.txt > sx.tmp && sort y.txt > sy.tmp && join -1 3 -2 1 -o "1.1 1.2 1.3 2.2" sx.tmp sy.tmp | sort -r && rm -fr *.tmp
y 13 ay TT
x 12 ax NN
it's no need to use awk! using join is ok, :-) and how to use awk?

.Aaron
  #3 (permalink)  
Old 03-16-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
Quote:
Originally Posted by yunccll View Post
it's no need to use awk! using join is ok, :-) and how to use awk?

.Aaron
But you're using join, 3 times sort and 2 temporary files!

With awk:

Code:
awk 'NR==FNR{a[$1]=$2;next}$3 in a{print $0 a[$1]" "a[$3]}' y.txt x.txt
Regards
  #4 (permalink)  
Old 03-17-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,854
If order matters:

Code:
awk 'NR==FNR{x[$3]=$0;next}$1 in x&&$0=x[$1] FS$2' x.txt y.txt
Use nawk or /usr/xpg4/bin/awk on Solaris.
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 09:59 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