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
compare files in two directories and output changed files to third directory bkeep Shell Programming and Scripting 4 08-26-2009 07:58 AM
compare two files and to remove the matching lines on both the files shellscripter Shell Programming and Scripting 4 06-20-2009 09:48 AM
compare two txt files space13 Shell Programming and Scripting 8 09-22-2006 10:40 AM
help! need to compare files danielsf Shell Programming and Scripting 6 07-09-2003 06:09 PM
Compare two files b5fnpct UNIX for Dummies Questions & Answers 2 08-16-2001 04:28 PM

 
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
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 11-02-2009
gpaulose gpaulose is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 16
compare two files using awk

Hi,

I want to compare two files using awk and write an output based on if the records matched.

Both the files are space delimitted.

File A:

Code:
8351 00000000000636 2009044 -00001.000
8351 00000000000637 2009044 -00002.000
8351 00000000000638 2009044 -00001.000
8351 00000000000640 2009044 -00003.000
8351 00000000000642 2009044 -00001.000
8351 00000000000650 2009044 -00001.000

File B:


Code:
8351 00000000000636 006
8351 00000000000637 002
8351 00000000000642 001

Output:

Code:
006 8351 00000000000636 2009044 -00001.000
002 8351 00000000000637 2009044 -00002.000
000 8351 00000000000638 2009044 -00001.000
000 8351 00000000000640 2009044 -00003.000
001 8351 00000000000642 2009044 -00001.000
000 8351 00000000000650 2009044 -00001.000

Basically, if the second column of File A matches with the second column of File B. I need to write the third column of file B and the complete line from File A.

If not matched I need to write 000 and then the complete line in File A.

I tried some awk commands.. but didnt work. This below code i wrote to get the matched record and write the third column and the file A. But the third column is not getting written.

Any help will be much appreciated.


Code:
awk '
NR==FNR{a[$2]=$2;next}
a[$2]{print a[$3] $0}' $ZEROSTOCK $INFILE > $INTEMP


Last edited by vgersh99; 11-02-2009 at 04:54 PM.. Reason: code tags, please!
 

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