批处理文件:从.tsv文件中删除回车 [英] Batch File: Removing carriage return from .tsv file

查看:103
本文介绍了批处理文件:从.tsv文件中删除回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to remove carriage return from .tsv file using batch file. This is how my .tsv file looks, the first line is a column line

* ** [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]的内容(无任何替换)

I want this file as below [I used notepad to remove('replace' by nothing) occurrences of [CR] only]

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  

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

I tried following batch file. file is being put in one single line. It removes both carriage return and line feed.

@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

I want to modify the .bat file so that it only removes \r instead of removing both \r\n

更新:某种程度上我能够添加图像,这将使思路更清晰.相似的.tsv文件希望它像这样

Update: Somehow I am able to add images, this will give clearer idea. Similar .tsv file want it to be like this

推荐答案

如果您使用

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

You can overwrite the original file if you use /o -.

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

You must use CALL JREPL if you put the command within a batch script.

下面是我的原始答案,当我认为源文件中每行的末尾都有CR/LF(在编辑问题之前).

Below is my original answer for when I thought there was a CR/LF at the end of each line in the source file (before the question was edited).

我讨厌用批处理方式编辑文本文件,因为它需要大量的奥秘知识,有很多限制,而且结果很慢.但是,可以用批处理解决这个问题,我决定可以使用实践:-)

I hate editing text files with batch, as it requires a lot of arcane knowledge, has many restrictions, and the result is slow. However, it is possible to solve this with batch, and I decided I could use the practice :-)

下面的工作是假设每条输入线的长度是< = 1021字节,而输出线的全部是<〜8191字节长.

The following works provided that each input line is <= 1021 bytes long, and the output lines are all < ~8191 bytes long.

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

这篇关于批处理文件:从.tsv文件中删除回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆