ksh - Unix File Merge - If key exists replace line with value -
i have 2 files table.cols , table.rules
table.cols:
column_1 column_2 column_3 column_4 column_5
table.rules:
column_3 my_function(column_3, another_parameter) column_4 my_other_function(column_3, a_different_parameter)
i want merge these files produce:
column_1, column_2, my_function(column_3, another_parameter), my_other_function(column_3, a_different_parameter), column_5
notice commas @ end of each line except last.
try -
$ head file? ==> file1 <== column_3 my_function(column_3, another_parameter) column_4 my_other_function(column_3, a_different_parameter) ==> file2 <== column_1 column_2 column_3 column_4 column_5 $ awk -v line=$(wc -l < file2) 'nr==fnr{a[$1]=$2fs$3;next} {print (a[$1] ?a[$1]ofs :((fnr<line)?$0 ofs:$0))}' ofs=, file1 file2 column_1, column_2, my_function(column_3, another_parameter), my_other_function(column_3, a_different_parameter), column_5
explained :
((fnr<line)?$0 ofs:$0)) : ignore comma last line.
Comments
Post a Comment