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

我正在尝试使用批处理文件从.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文件在此处输入图片说明希望它像这样在此处输入图片说明

德本纳姆

如果您使用JREPL.BAT正则表达式文本处理实用程序,这将是微不足道的

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!"
  )
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章