在Windows批处理文件中使用sed或tr命令用空格替换双引号

时间:2017-06-09 07:35:42

标签: batch-file unix

我正在 Windows的批处理文件中编写脚本。我想用文件中的空格替换双引号。

输入文件File1.txt包含:

  

“05-09-2017”,“07:00:14”
  “05-09-2017”,“07:00:14”
  “05-09-2017”,“07:00:14”
  “05-09-2017”,“07:00:14”
  “05-09-2017”,“07:00:14”

我尝试了以下内容:

tr.exe "\"" " " < "File1.txt" >  "File2.txt"

以上一行给出了一个类似

的错误
  

tr.exe:参数太多

我也尝试过:

sed.exe "s/\"/ /g" "File1.txt" >  "File2.txt"

以上给我一个错误,如

  

sed.exe:无法读取&gt;:参数无效

我需要这样的输出文件:

  

05-09-2017,07:00:14
  05-09-2017,07:00:14
  05-09-2017,07:00:14
  05-09-2017,07:00:14
  05-09-2017,07:00:14

请你好好研究一下。

2 个答案:

答案 0 :(得分:1)

sed "s/\x22/ /g" "input.txt" 

tr "\""" " "   < "input.txt"
tr """" " "    < "input.txt"
tr \""  " "    < "input.txt"

答案 1 :(得分:0)

为什么在这个简单的任务中使用Unix工具而不是本机Windows命令?

@echo off
if not exist "File1.txt" goto :EOF
setlocal EnableDelayedExpansion
del "File2.txt" 2>nul
for /F "usebackq delims=" %%I in ("File1.txt") do (
    set "Line=%%I"
    set "Line=!Line:"= !"
    echo !Line!>>"File2.txt"
)
endlocal

注意:此处使用的命令 FOR 会跳过以分号开头的空行和行。

我会替换双引号,这意味着在等号后删除空格字符。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

另请阅读Microsoft有关Using Command Redirection Operators

的文章