使用Windows批处理文件删除尾随空格? [英] Remove trailing spaces from a file using Windows batch?

查看:308
本文介绍了使用Windows批处理文件删除尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能削减从一个文本文件中使用Windows命令提示符所有尾随空格?

How could I trim all trailing spaces from a text file using the Windows command prompt?

推荐答案

该DosTips RTRIM函数本霍金被引频次,可用于创建一个脚本,可以正确修剪在文本文件中的每一行。但是,该函数是比较慢的。

The DosTips RTRIM function that Ben Hocking cites can be used to create a script that can right trim each line in a text file. However, the function is relatively slow.

DosTips用户(和主持人)aGerman开发了非常有效的正确的修剪算法。他实现的算法作为批处理宏观 - 存储在可以从存储器执行环境变量复杂迷你脚本的一个有趣的概念。带有参数的宏都在自己的和主要讨论的话题未相应和这个问题。

DosTips user (and moderator) aGerman developed a very efficient right trim algorithm. He implemented the algorithm as a batch "macro" - an interesting concept of storing complex mini scripts in environment variables that can be executed from memory. The macros with arguments are a major discussion topic in and of themselves that is not relevent to this question.

我已经提取aGerman的算法,并把它放在下面的批处理脚本。该脚本需要一个文本文件作为唯一的参数的名称,并继续向右修剪空间关闭文件中的每一行。

I have extracted aGerman's algorithm and put it in the following batch script. The script expects the name of a text file as the only parameter and proceeds to right trim the spaces off each line in the file.

@echo off
setlocal enableDelayedExpansion
set "spcs= "
for /l %%n in (1 1 12) do set "spcs=!spcs!!spcs!"
findstr /n "^" "%~1" >"%~1.tmp"
setlocal disableDelayedExpansion
(
  for /f "usebackq delims=" %%L in ("%~1.tmp") do (
    set "ln=%%L"
    setlocal enableDelayedExpansion
    set "ln=!ln:*:=!"
    set /a "n=4096"
    for /l %%i in (1 1 13) do (
      if defined ln for %%n in (!n!) do (
        if "!ln:~-%%n!"=="!spcs:~-%%n!" set "ln=!ln:~0,-%%n!"
        set /a "n/=2"
      )
    )
    echo(!ln!
    endlocal
  )
) >"%~1"
del "%~1.tmp" 2>nul

假设脚本名为rtrimFile.bat,那么它可以从命令行调用如下:

Assuming the script is called rtrimFile.bat, then it can be called from the command line as follows:

rtrimFile "fileName.txt"

的说明有关性能

原来DosTips RTRIM函数执行线性搜索和缺省修剪最多32位。它具有每空迭代一次。

A note about performance
The original DosTips rtrim function performs a linear search and defaults to trimming a maximum of 32 spaces. It has to iterate once per space.

aGerman的算法使用二进制搜索,它能够在13次迭代修剪分批允许(可达8K〜空格)的最大字符串大小。

aGerman's algorithm uses a binary search and it is able to trim the maximum string size allowed by batch (up to ~8k spaces) in 13 iterations.

Unfotunately,一批很慢,当涉及到处理文本。即使高效RTRIM函数,它需要〜70秒,修剪我的机器上1MB的文件。问题是,只是读取和写入文件时不进行修改需要时间显著。这个答案使用for循环读取文件,再加上FINDSTR至preFIX与行号的每一行,使空行是preserved。它切换延迟扩展prevent 被破坏,并使用搜索​​和替换操作,从每一行中删除的行号preFIX。所有这一切之前就开始做RTRIM。

Unfotunately, batch is very SLOW when it comes to processing text. Even with the efficient rtrim function, it takes ~70 seconds to trim a 1MB file on my machine. The problem is, just reading and writing the file without any modification takes significant time. This answer uses a FOR loop to read the file, coupled with FINDSTR to prefix each line with the line number so that blank lines are preserved. It toggles delayed expansion to prevent ! from being corrupted, and uses a search and replace operation to remove the line number prefix from each line. All that before it even begins to do the rtrim.

性能可以通过使用备用文件读取机制近一倍使用设置/ p 。然而,设定/ p方法仅限于〜1K字节每行,这条每一行尾部的控制字符。

Performance could be nearly doubled by using an alternate file read mechanism that uses set /p. However, the set /p method is limited to ~1k bytes per line, and it strips trailing control characters from each line.

如果您需要定期修剪大文件,则表现甚至翻倍可能是不足够的。时间下载(如果可能),可以处理文件中的眨眼的许多公用事业任何一个。

If you need to regularly trim large files, then even a doubling of performance is probably not adequate. Time to download (if possible) any one of many utilities that could process the file in the blink of an eye.

如果您不能使用非原生软件,那么你可以尝试VBScript或JScript通过CSCRIPT批处理命令excecuted。要么一会快很多。

If you can't use non-native software, then you can try VBScript or JScript excecuted via the CSCRIPT batch command. Either one would be MUCH faster.

更新 - 与JREPL.BAT快速的解决方案

JREPL.BAT 是一个普通的前pression找到/替换工具,可以非常有效地解决这个问题。它是纯粹的脚本(混合批次/ JScript中)的本地运行任何Windows机器上从XP起。任何第三方exe文件是必要的。

JREPL.BAT is a regular expression find/replace utility that can very efficiently solve the problem. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe files are needed.

使用JREPL.BAT您的PATH中的某个地方,你可以去除尾随文件的test.txt空间这个简单的命令:

With JREPL.BAT somewhere within your PATH, you can strip trailing spaces from file "test.txt" with this simple command:

jrepl " +$" "" /f test.txt /o -

如果你把一个批处理脚本中的命令,则必须precede与调用命令:

If you put the command within a batch script, then you must precede the command with CALL:

call jrepl " +$" "" /f test.txt /o -

这篇关于使用Windows批处理文件删除尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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