3

I am trying to combine a a file which has the header and another file which has the data component. For example…

header.bcp (all on one line, not restricted to 4 columns):

SNO,Name,Age,Gender ......

data.bcp (multiple lines with the number of fields same as columns):

1,"ABC",20,"M" .....
2,"XYZ",22,"F" .....

The expected output should be:

SNO=1,Name="ABC",Age=20,Gender="M" ....
SNO=2,Name="XYZ",Age=22,Gender="F" ....

My guess was with awk, any quick solutions, thoughts ?

2

Quick 'n' dirty solution if the fields cannot include embedded commas as mentioned in the comments:

awk '
  BEGIN {FS=","; OFS=FS} 
  NR==FNR {split($0,a); next} 
  {
    for(i=1; i<=NF;i++) $i=a[i]"="$i;
  } 1
' header.bcp data.bcp
  • @xhienne no sorry - overthinking it I guess. Please see edited version. – steeldriver Jan 18 '17 at 17:35
  • @xhienne ah that's good to know - so if the third argument is empty, it uses the current FS rather than the default FS I guess? – steeldriver Jan 18 '17 at 17:41
  • Exactly, and this is even POSIX-compliant – xhienne Jan 18 '17 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.