I'm looking for a straightforward console solution to change a text file which looks like this:
...
Gender: M
Age: 46
History: 01305
Gender: F
Age: 46
History: 01306
Gender: M
Age: 19
History: 01307
Gender: M
Age: 19
History: 01308
....
To csv file like this one:
Gender,Age,History
M,46,01305
F,46,01306
M,19,01307
M,19,01308
Any help appreciated
With following solutions I've received this output. Am I doing something wrong?
awk 'BEGIN{printf "Gender,Age,History%s",ORS;FS=":"}{c++} {sub(/^ */,"",$2);printf "%s%s",$2,(c==3)?ORS:","}c==3{c=0}' data.txt >> 1.csv
Gender,Age,History
M
,37
,00001
M
,37
,00001
M
,41
,00001
:-)awk 'BEGIN{printf "Gender,Age,History%s",ORS} {FS=":";count++} {sub(/^ */,"",$2);printf "%s%s",$2,(cou nt==3)?ORS:","}count==3{count=0}' filecountabove. The space should be removed. It is even better to put the field separator in theBEGINblock as inawk 'BEGIN{printf "Gender,Age,History%s",ORS;FS=":"}{c++} {sub(/^ */,"",$2);printf "%s%s",$2,(c==3)?ORS:","}c==3{c=0}' file