I want to use AWK to transform a CSV file to TOML. My input looks like this:
id , name , lifetime
adam , Adam , 1550-1602
eve , Eve , 1542-1619
and I'm trying to get to this
[adam]
name = "Adam"
lifetime = "1550-1602"
[eve]
name = "Eve"
lifetime = "1542-1619"
I made the following little AWK script, which doesn't quite make it:
BEGIN {
FS=","
}
NR == 1 {
nc = NF
for (c = 1; c <= NF; c++) {
h[c] = $c
}
}
NR > 1 {
for(c = 1; c <= nc; c++) {
printf h[c] "= " $c "\n"
}
print ""
}
END {
}
The result so far is this
id = adam
name = Adam
lifetime= 1550-1602
id = eve
name = Eve
lifetime= 1542-1619
For the record my version of AWK is GNU Awk 4.1.4
Could you please try following, written and tested with shown samples in GNU
awk
.Explanation: Adding detailed explanation for above solution.
NOTE: OP's sample Input_file was having spaces in first line to remove them
gsub(/^ +| +$/,"",$i)
is being used remove this if no spaces are found at last of 1st line of Input_file.