批处理文件:从.tsv文件中删除回车

时间:2015-09-30 14:34:48

标签: file batch-file

我正在尝试使用批处理文件从.tsv文件中删除回车。 这就是我的.tsv文件的外观,第一行是列线

手动添加这些行中显示的

* ** [CR] [LF] 以获得想法

Class   Name & Address  Item    lbs Value   Pickup/Drop off Date:   23 Sep[CR][LF]
Class1  Ben Coha[CR]
 2305 E LA st[CR]
 VISIA, PA[CR]
 932112-4422    Health and beauty product / cologne body wear for men   0.13    19[CR][LF]      
Class2  mich marce[CR]
 255 rid court[CR]
 prince frick, PA[CR]
 20442  health and beauty product / cologne body wear for women 1.5 47  

我想要这个文件如下[我用记事本删除('替换'什么都没有)仅出现[CR]

Class   Name & Address  Item    lbs Value   Pickup/Drop off Date:   23 Sep[LF]
Class1  Ben Coha 2305 E LA st VISIA, PA 932112-4422 Health and beauty product / cologne body wear for men   0.13    19[LF]      
Class2  mic marce 255 rid court prince frick, PA 20442  health and beauty product / cologne body wear for women 1.5 47  

我尝试了以下批处理文件。文件放在一行中。它会删除回车和换行。

@echo off
SetLocal DisableDelayedExpansion
for /f "delims=" %%a in (myFile.tsv) do (
echo/|set /p ="%%a%"
)>>newMyFile.tsv    

结果看起来像..

Class   Name & Address  Item    lbs Value   Pickup/Drop off Date:   23 SepClass1    Ben Coha 2305 E LA st VISIA, PA 932112-4422 Health and beauty product / cologne body wear for men   0.13    19      Class2  mic marce 255 rid court prince frick, PA 20442  health and beauty product / cologne body wear for women 1.5 47  

我想修改.bat文件,以便它只删除\ r而不是删除\ r \ n

更新:不知何故,我能够添加图像,这将提供更清晰的想法。 类似.tsv文件 enter image description here 希望它像这样 enter image description here

3 个答案:

答案 0 :(得分:2)

你能用空的第一线吗?

@echo off
SetLocal EnableDelayedExpansion
(
  for /f "delims=" %%a in (myFile.tsv) do (
    set "line=%%a"
    if "!line:~0,1!" neq " " (
      echo(!newline!
      set "newline=!line!"
    ) else (
      set "newline=!newline!!line!"
    )
  )
  echo(!newline!
)>newMyFile.tsv  

答案 1 :(得分:2)

如果您使用JREPL.BAT regular expression text processing utility

,这是微不足道的
jrepl \r "" /f "myFile.tsv" /o "newMyFile.tsv"

如果您使用/o -,则可以覆盖原始文件。

如果将命令放在批处理脚本中,则必须使用CALL JREPL

以下是我原来的答案,当我认为源文件中每行末尾有一个CR / LF时(在编辑问题之前)。

我讨厌用批量编辑文本文件,因为它需要很多神秘的知识,有很多限制,结果很慢。但是,有可能用批处理解决这个问题,我决定使用这种做法: - )

以下工作规定,每条输入行的长度为< = 1021字节,输出行全部为< 〜8191字节长。

@echo off
setlocal enableDelayedExpansion
set "input=test.txt"
set "output=out.txt"

:: Define LF to contain a linefeed character
set ^"LF=^

^"  The empty line above is critical - DO NOT REMOVE

:: Determine how many sets of 4 lines must be read
for /f %%N in ('find /c /v "" ^<"test.txt"') do set /a cnt=%%N/4

<"!input!" >"!output!" (

  %= Read and write the first line =%
  set "ln="
  set /p "ln="
  <nul set /p "=!ln!!LF!"

  %= Outer loop iterates the 4 line sets =%
  for /l %%N in (1 1 !cnt!) do (

    %= Initialize out line to empty =%
    set "out="

    %= Inner loop appends next 4 lines into out =%
    for /l %%n in (1 1 4) do (
      set "ln="
      set /p "ln="
      set "out=!out!!ln!"
    )

    %= Write the line =%
    <nul set /p "=!out!!LF!"
  )
)

答案 2 :(得分:2)

也许这样的事情对你有用,它只是简单地替换所有CR

setlocal DisableDelayedExpansion
for /F "usebackq" %%C in (`copy /Z "%~dpf0" nul`) DO (
    for /F "delims=" %%L in (myFile.tsv) do (
      set "line=%%L"
      if defined line (
          setlocal EnableDelayedExpansion
          echo(!line:%%C=!
          endlocal
      ) ELSE echo(
    )
)
endlocal