Windows批处理读取TXT并将行解析为CSV


1

通过搜索该站点并学习Windows批处理脚本的荒谬语言,我取得了很大进展,但坦率地说,我陷入了困境。以下示例来自我们实验室的旧辐射计模块,该模块定期将其数据记录为TXT文件。我正在尝试将它们解析为适当的CSV。想法是拥有一个.bat文件,该文件可以定期运行以将特定文件夹中的所有TXT日志更改为CSV格式。

我认为可以这样做:删除空行(= header)之前的所有内容,用制表符替换空格(这是必要的,因为值之间的间距不一致),用逗号替换制表符,摆脱双逗号。

*。文本:

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,... 
...

这里有人知道批处理并且可以让我入门吗?

Answers:


0

命令行:

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

要么:

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

powershell脚本:

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

要么:

$h=25;$f=gc data.txt;$f[$h..($f.count-1)]|%{('"'+$_.Trim()+'"')-replace'\s+','";"'>>data.csv }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.