Jonas Stein
Jonas Stein

Reputation: 7043

convert .csv with bash tools

German version of Origin [TM] saves .csv files very odd. However I have many of these .csv files and want to convert them with a script.

A,B
Independent variable,comment1 
"0,4","0,067"
"0,0","0,08"
"0,07","0,02"
"0,09","0,00"

should become

A,B
# Independent variable,comment1 
0.4,0.067
0.0,0.08
0.07,0.02
0.09,0.00

The definition of a comment by Origin seems to be

it is the second line

Upvotes: 0

Views: 317

Answers (5)

Tedee12345
Tedee12345

Reputation: 1330

Another solution:

awk -F\" '{$1=$1; gsub(/0,/,"0."); gsub(/ /,""); if(NR==2) printf "%s ", "#"}1' file

Upvotes: 0

Steve
Steve

Reputation: 54452

If there is structure to your csv files, and Kevin's comment above is correct, then you can use ranges by line number:

sed '2s/^/# /; 3,$s/"\([^,]*\),\([^,]*\)"/\1.\2/g' file

Or a slightly shorter way:

sed '2s/^/# /; 3,$s/,/./g;s/"."/,/g;s/"//g' file

Results:

A,B
# Independent variable,comment1 
0.4,0.067
0.0,0.08
0.07,0.02
0.09,0.00

Upvotes: 4

Ed Morton
Ed Morton

Reputation: 203792

If you like simple solutions, here's awk:

$ cat tst.awk
BEGIN{FS="\",\""; OFS=","}
NR == 2 { $0 = "# " $0 }
NR >= 3 {
   for (i=1;i<=NF;i++) {
      sub(/\"/,"",$i)
      sub(/,/,".",$i)
   }
}
{ print }

$ awk -f tst.awk file
A,B
# Independent variable,comment1
0.4,0.067
0.0,0.08
0.07,0.02
0.09,0.00

"NR" is the line number and "NF" is the number of ","-separated fields on the current line. The rest should be obvious, I think, even if you don't know awk.

Upvotes: 2

Vijay
Vijay

Reputation: 67271

perl -pe 's/\"//g;if($.==2){s/^/#/g;}' your_file

if you want to do an in place replacement:

perl -pi -e 's/\"//g;if($.==2){s/^/#/g;}' your_file

awk:

awk '{gsub(/\"/,"");if(NR==2)$0="#"$0;print}' your_file

Upvotes: 1

perreal
perreal

Reputation: 98018

With sed, assuming comment lines have the word comment:

sed 's/"\([0-9]*\),\([0-9]*\)"/\1.\2/g;/comment/{s/^/#/}' input

Assuming comments follow single capital-case variable names:

sed 's/"\([0-9]*\),\([0-9]*\)"/\1.\2/g;/^[A-Z],/{n;s/^/#/}' inpu

Upvotes: 2

Related Questions