![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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
|
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|