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
File Comparison- Need help uihnybgte Shell Programming and Scripting 8 04-13-2009 05:04 AM
tsv file comparison Swapna173 UNIX for Dummies Questions & Answers 0 03-25-2009 03:44 AM
file comparison satish.res Shell Programming and Scripting 7 10-09-2008 08:55 AM
file comparison...help needed. er_ashu UNIX for Dummies Questions & Answers 4 05-15-2008 10:37 PM
File Comparison net_shree Shell Programming and Scripting 19 01-10-2008 08:00 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 04-30-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
awk program for file comparison

Hello there,


I'm trying to write an awk program in bash shell with the following three input files:


File 1
1001 1 2 3
1002 4 5 6
1003 7 8 9
1004 10 11 12

File 2
1001 11 22 33
1002 44 55 66
1004 100 111 122

File 3
1001 111 222 333
1004 130 141 152

I would like to compare the first field of File3 with the first field of File 2. If a particular first field in File 2 doesn’t exist in File 3, then I want to delete that entire row in File 2.

For example, first field on Row 2 of File 2 (=1002) doesn’t exist in File 3. Hence I want to delete this row in File 2 and save it.

Now I want to compare File 3 and File 1. We see that the first field of second row (=1002) and the first field of the third row (=1003) of File 1 doesn’t exist in File 3. I want to delete these two rows from File 1 and save it.

After the above steps, the three files would result as the following:

File 1
1001 1 2 3
1004 10 11 12

File 2
1001 11 22 33
1004 100 111 122

File 3
1001 111 222 333
1004 130 141 152


Any help is greatly appreciated.
  #2 (permalink)  
Old 04-30-2009
ldapswandog ldapswandog is offline
Registered User
  
 

Join Date: Feb 2009
Location: Charlotte, NC
Posts: 101
You cna use the join command and do what your looking for using the same steps you describe.

Code:
join -1 1 -2 1 -t " " -o 2.1,2.2,2.3,2.4 file3 file2 > newFile2
join -1 1 -2 1 -t " " -o 2.1,2.2,2.3,2.4 file3 file1 > newFile1

# -1 1  use column one of first file 'file3'
# -2 1  use column one of second file 'file2' or 'file1'
# -t " " use {space} as a delimter
# -o use fields 1,2,3,4 from second file as output

  #3 (permalink)  
Old 04-30-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Hi ldapswandog,

Thanks a lot for the info. I did try out the command 'join' as you mentioned but I got the following result for the newfile2.

1001 11
1004 100

instead of:

1001 11 22 33
1004 100 111 122

Am I doing something wrong?

Appreciate your guidance.
  #4 (permalink)  
Old 05-01-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514

Code:
 $ join -1 1 -2 1 -o 2.1 2.2 2.3 2.4 file3 file1

1001 1 2 3
1004 10 11 12



Code:
$ join -1 1 -2 1 -o 2.1 2.2 2.3 2.4 file3 file2

1001 11 22 33
1004 100 111 122


cheers,
Devaraj Takhellambam
  #5 (permalink)  
Old 05-01-2009
kbirde kbirde is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Hi Devaraj,

Thanks so much. It is working now. I guess the -t " " was the issue (but don't know why).

Regards,
kbirde
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 01:12 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