Friday, 15 January 2010

Using AWK to Process Input from Multiple Files -



Using AWK to Process Input from Multiple Files -

many people have been helpful posting next solution awk'ing multiple input files @ once:

$ awk 'fnr==nr{a[$1]=$2 fs $3;next}{ print $0, a[$1]}' file2 file1

this works well, wondering if explain me why? find awk syntax little bit tough hang of , hoping wouldn't mind breaking code snippet downwards me. give thanks time , help!

awk 'fnr==nr{a[$1]=$2 fs $3;next}

here handle 1st input (file2). say, fs space, build array(a) up, index column1, value column2 " " column3 fnr==nr , next means, part of codes work file2. man gawk check nr , fnr

{ print $0, a[$1]}' file2 file1

when nr != fnr it's time process 2nd input, file1. here print line of file1, , take column1 index, find out value in array(a) print. in word, file1 , file2 joined column1 in both files.

for nr , fnr, shortly,

1st input has 5 lines 2nd input has 10 lines, nr 1,2,3...15 fnr 1...5 1...10

you see trick of fnr==nr check.

awk

No comments:

Post a Comment