Separate all characters in a string with FS (awk)

advertisements

I though it would have been a simple task but nope !

input (pipe separated):

record1|ABCDEFGH
record2|IJKLMNOP

I would need this output:

record1|A|B|C|D|E|F|G|H
record2|I|J|K|L|M|N|O|P

What I tried so far:

awk 'BEGIN{FS=OFS="|"}{a = split($2, b, ""); for(i=1; i<=a; i++){printf("%s|%s, $1, b[i](i==a? "\n":"|"))}}' input

and I get:

record1|A
record1|B
record1|C
...

I don't get it! If someone could explain it to me...


Your printfs are all over the shop. With modifications as shown below, it works as expected

awk 'BEGIN{FS=OFS="|"}{a = split($2, b, "");
   printf "%s|", $1;
   for(i=1; i<=a; i++){printf("%s%s", b[i],i==a? "\n":"|")}}' input