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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to compare a field value of a same column? gobinath Shell Programming and Scripting 6 05-06-2009 02:39 AM
compare the column from 3 files and merge that line ganesh_mak Shell Programming and Scripting 8 04-14-2008 08:56 AM
awk compare column between 2 files phamp008 Shell Programming and Scripting 3 01-18-2008 12:24 AM
I need to extract last column of a file and compare the values vukkusila Shell Programming and Scripting 4 08-04-2007 12:21 PM
Compare Column value from Two Different Files hanie123 Shell Programming and Scripting 7 04-02-2007 10:34 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 12-09-2008
tiggyboo tiggyboo is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 2
awk column compare

I've been banging my head against the wall to accomplish the following. Given two files:

File a.txt

12345 hello
32324 there

File b.txt

12345 stuff
45454 howdy
32324 joe

If column 1 matches between the two files, then print only the entire line of the 2nd file (b.txt in this example So the desired output in this case would be:

12345 stuff
32324 joe

I've been working with the follow as a basic start, but seem to have come up with every combination except that which I'm looking for:

awk 'NR==FNR{++a[$1];next}!a[$1]' a.txt, b.txt > c.txt

As you probably guessed this is based on an 'almost' solution google search - in the meantime I've purchased the O'Reilly book and am plowing through it - but in the meantime I really need to get this done... so, anyone kind enough to share a (hopefully somewhat commented) snippet would be most appreciated.

Thanks,
Al
  #2 (permalink)  
Old 12-09-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
awk 'NR==FNR{++a[$1];next} $1 in a' a.txt b.txt > c.txt

  #3 (permalink)  
Old 12-10-2008
tiggyboo tiggyboo is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 2
Quote:
Originally Posted by vgersh99 View Post
Code:
awk 'NR==FNR{++a[$1];next} $1 in a' a.txt b.txt > c.txt
Thanks, just what I was looking for!
Al
  #4 (permalink)  
Old 12-09-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,418
if you have a sorted files then you can go for much simpler command like "comm"
Closed Thread

Bookmarks

Tags
awk, column, linux, regex

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:37 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