![]() |
|
|
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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
using awk
Hi,
I have one file like below: 0744 111305 9.92 52008 1552 111305 1.70 52008 5345 111305 0.40 52008 1552 111305 1.58 52056 and other file like below: 1552 name2 0744 name3 5345 name4 I need to paste the "name" column in the second file to the fist file if there is any match on the fist column. I can't use paste command since first file will have duplicated ID on the first column. This is what I'd like to show on the final file: 0744 name3 11305 9.92 52008 1552 name2 111305 1.70 52008 5345 name4 111305 0.40 52008 1552 name2111305 1.58 52056 Any help would be very appreciated! |
|
||||
|
There is command in UNIX which does a "join" on files using their columns (analogous to database table joins). The only condition being the fields on which join to be performed should be sorted.
Lets call the first file "file1" and the second file "file2". Heres what you can do: sort file1>sfile1;sort file2>sfile2; join -a1 -o 1.1,1.2,1.3,1.4,2.2 sfile1 sfile2 Explanation: -o lets you specify which columns you want to see in ur output. Columns belonging to the first file are denoted by 1 followed by a decimal point and the column number. Columns belonging to the second file are denoted by 2 followed by a decimal point and the column number. So here we trying to see all columns from the first file and the second column from the second file in the order specified. The -a flag prints unpaired lines from file1. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|