I am trying to use AWK to process a comma seperated text file and insert new columns. I want to split the contents of the second column into three parts and insert three columns after column B in a new text file.
insert.txt - my data
column A, name|ID1234 @ tag1, column C, column D
Output I am trying produce
column A, name|ID1234 @ tag1, name, ID1234, tag1, column C, column D
Code I have tried
#!/bin/awk
BEGIN {
FS=","
OFS=","
}
{
# Split Column B - twice
split($2,a,"|") #a[1] a[2] seperated by |
split(a[2],b,"@") #b[1] b[2] seperated by @
print $1,$2,a[1],b[1],b[2];
{for(i=3;i<=NF;++i) printf $i}
}
awk -f insert.awk insert.txt > output.txt
How do I format the output to all be on a single line, comma separated with the inserted columns?
Is it possible during output to trim the white space at the begin and end of each column?
Thanks Joshua