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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -