Issue
I have the data (test.txt) in the below format.
x: a
y: b
I need it in this format. (1st thing that comes before: is the header, after the space the value is data) (edited)
x,y
a,b
I can do like
cat test.txt | tr ": " '\n'
x
a
y
b
It just comes in the new line. Is there any way I can achieve the desired format?
Solution
$ cat tst.awk
BEGIN {
FS = ": *"
OFS = ","
}
{
for (i=1; i<=NF; i++) {
vals[NR,i] = $i
}
}
END {
for (i=1; i<=NF; i++) {
for (j=1; j<=NR; j++) {
printf "%s%s", vals[j,i], (j<NR ? OFS : ORS)
}
}
}
$ awk -f tst.awk test.txt
x,y
a,b
Answered By - Ed Morton Answer Checked By - Marilyn (WPSolving Volunteer)