1

I've made a good deal of headway by searching this site and learning the ridiculous language that is Windows batch scripting, but frankly I'm stuck. The sample below is from our lab's old radiometer module which regularly logs its data as TXT files. I'm trying to parse them into proper CSV. The idea is to have a .bat file that can periodically run to change all the TXT logs in a certain folder to CSV format.

The way I figure it might be done: Delete everything before the empty line (=header), replace spaces by tabs (necessary because the spacing between values is inconsistent), replaces tabs by comma's, get rid of double comma's.

*.txt:

PRODUCES
    MFRSR Detector Temp ,  degC
    HTR V ,  V
    MFRSR Thermopile Total ,  W/m^2
    MFRSR 414.0nm Total ,  (W/m^2)/nm
    MFRSR 496.6nm Total ,  (W/m^2)/nm
    MFRSR 613.0nm Total ,  (W/m^2)/nm
    MFRSR 670.9nm Total ,  (W/m^2)/nm
    MFRSR 869.3nm Total ,  (W/m^2)/nm
    MFRSR 937.6nm Total ,  (W/m^2)/nm
    MFRSR Thermopile Diffuse ,  W/m^2
    MFRSR 414.0nm Diffuse ,  (W/m^2)/nm
    MFRSR 496.6nm Diffuse ,  (W/m^2)/nm
    MFRSR 613.0nm Diffuse ,  (W/m^2)/nm
    MFRSR 670.9nm Diffuse ,  (W/m^2)/nm
    MFRSR 869.3nm Diffuse ,  (W/m^2)/nm
    MFRSR 937.6nm Diffuse ,  (W/m^2)/nm
    MFRSR Thermopile DirNorm ,  W/m^2
    MFRSR 414.0nm DirNorm ,  (W/m^2)/nm
    MFRSR 496.6nm DirNorm ,  (W/m^2)/nm
    MFRSR 613.0nm DirNorm ,  (W/m^2)/nm
    MFRSR 670.9nm DirNorm ,  (W/m^2)/nm
    MFRSR 869.3nm DirNorm ,  (W/m^2)/nm
    MFRSR 937.6nm DirNorm ,  (W/m^2)/nm

    42099   0.29236 -0.55603     45.20     7.4819          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5          *5
    42099   0.57222 0.01180  45.20     7.8285      0.9378      0.0086      0.0086      0.0032      0.0040      0.0018      0.0005      0.9473      0.0093      0.0086      0.0032      0.0040      0.0018      0.0005     -0.8090      0.0000      0.0000      0.0000      0.0000      0.0000      0.0000
    42099   0.57291 0.01415  45.20     7.8382      1.3587      0.0093      0.0086      0.0032      0.0040      0.0018      0.0005      1.3725      0.0093      0.0086      0.0032      0.0040      0.0023      0.0005     -0.9770      0.0000      0.0000      0.0000      0.0000      0.0000      0.0000
    42099   0.57361 0.01656  45.20     7.8998      2.6372      0.0099      0.0100      0.0032      0.0040      0.0023      0.0005      2.6641      0.0099      0.0100      0.0032      0.0040      0.0023      0.0005     -1.6210      0.0000      0.0000      0.0000      0.0000      0.0000      0.0000

*.csv:

42099,0.29236,-0.55603,45.20,7.4819,*5,*5,*5,...
42099,0.57222,0.01180,45.20,7.8285,0.9378,0.0086,... 
42099,... 
...

Is there anyone here who knows batch and can get me started?

1 Answer 1

0

command-line:

powershell $h=25;$f=gc data.txt;$f[$h..($f.count-1)]^|%{$_.Trim()-replace'\s+',','>>data.csv }

or:

powershell $h=25;$f=gc data.txt;$f[$h..($f.count-1)]^|%{('\"'+$_.Trim()+'\"')-replace'\s+','""";"""'^|ac data.csv}

powershell script:

$h=25;$f=gc data.txt;$f[$h..($f.count-1)]|%{$_.Trim()-replace'\s+',','>>data.csv}

or:

$h=25;$f=gc data.txt;$f[$h..($f.count-1)]|%{('"'+$_.Trim()+'"')-replace'\s+','";"'>>data.csv }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .