I have this CSV files :
Monday,linux,6,0.2 Tuesday,linux,0.25,0.2 Wednesday,linux,64,3
I create a little script that allow me to recover the informations from my csv and to place them like this :
Day : Monday OS : Linux RAM : 6 CPU1 : 0.2
My script is :
#!/bin/bash awk -F'[ ,;|.]' 'FNR==0{next} FNR>1 { print "DAY : " $1; print "OS :n " $2 print "RAM :n " $3 print "CPU1 :n " $4 }' mycsvfile.csv
But the result is :
DAY : Tuesday OS : linux RAM : 0 CPU1 : 25 DAY : Wednesday OS : linux RAM : 64 CPU1
Or I want :
DAY : Monday OS : linux RAM : 0.2 CPU 1 : 1 DAY : Tuesday OS : linux RAM : 0.25 CPU 1 : 0.2 DAY : Wednesday OS : linux RAM : 64 CPU 1 : 3
Can you tell me why my script doesn’t works and why floats are not taken into account ?
Thank you !
Advertisement
Answer
Added tab and newline to same awk as Cyrus posted.
awk -F ',' '{ print "DAY :",$1 print "OS :",$2 print "RAM :",$3 print "CPU1 :",$4"n" }' OFS='t' file DAY : Monday OS : linux RAM : 6 CPU1 : 0.2 DAY : Tuesday OS : linux RAM : 0.25 CPU1 : 0.2 DAY : Wednesday OS : linux RAM : 64 CPU1 : 3
A more generic solution:
awk -F, 'BEGIN {split("DAY OS RAM CPU", header, " ")}{for (i=1;i<=4;i++) print header[i]":t",$i;print ""}' t DAY: Monday OS: linux RAM: 6 CPU: 0.2 DAY: Tuesday OS: linux RAM: 0.25 CPU: 0.2 DAY: Wednesday OS: linux RAM: 64 CPU: 3
More readable:
awk -F, ' BEGIN {split("DAY OS RAM CPU", header, " ")} { for (i=1;i<=4;i++) print header[i]":t",$i; print "" }' file