![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
shell or loop?
Hi,
I am new to shell and awk. Can anyone give me a hint where I should start with it? Thanks! |
|
#3
|
||||
|
||||
|
nawk -f cin.awk otherFile oneFile
with cin.awk being: Code:
NR==FNR { arr[$1]=$2; next }
$1 in arr { $1 = $1 OFS arr[$1] }
1
|
|
#4
|
|||
|
|||
|
nawk NR==FNR
Hi,
Thanks a lot! It worked. I am wondering if you could explain with plain English because I like to know how I can modify the code for my program. Thanks again! |
|
#5
|
||||
|
||||
|
here's the code with embedded comments:
Quote:
Last edited by vgersh99; 11-28-2005 at 09:26 PM. |
|
#6
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|