linux - Combine (Merge) Multiple Row using AWK -


hi have 1 awk command combine 2 files have same key.

awk -v ofs='\t' ' nr==1   { print $0, "column4", "column5"; next } nr==fnr { a[$1]=$0; next} $1 in { print a[$1], $2, $3 } ' $1 $2 > $3 

this return 1 key each files. example below,

file 1

key    column1  column2  column3   test1    500     400     200                test1    499     400     200                test1    499     399     200                test1    498     100     100                test2    600     200     150                test2    600     199     150                test2    599     199     100                

file 2

test1                            test2        

then results be

key    column1  column2  column3  column4  column5 test1    500     400     200               test2    600     200     150            

but want make rows have combined below.

key    column1  column2  column3  column4  column5 test1    500     400     200                        test1    499     400     200                         test1    499     399     200                         test1    498     100     100                         test2    600     200     150                         test2    600     199     150                          test2    599     199     100                       

anyone has idea change logic using awk. thank you!c

i think you're looking for

join file1 file2 

if insist on doing awk, way process files other way around, have parts want add ready when process main file:

awk -v ofs='\t' '   fnr == nr { a[$1] = $2 ofs $3; next }   { $1 = $1 }   fnr ==  1 { print $0, "column4", "column5" }   fnr !=  1 { print $0, a[$1] }   ' "$2" "$1" > "$3" 

edit: @etanreisner suggested addition of { $1 = $1 }. purpose of force awk rebuild line fields, input data split mixture of whitespaces comes out uniformly separated ofs (tab in case). if data tab-separated, not necessary (but doesn't hurt).


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -