简体   繁体   English

如何使用批处理从文本文件中删除回车符和换行符?

[英]How to remove carriage return and line feed characters from a text file using batch?

I've a fixed width text file so it contains leading zeros and spaces and I need to remove carriage return and line feed characters from the file.我有一个固定宽度的文本文件,所以它包含前导零和空格,我需要从文件中删除回车符和换行符。 Could you please let me know how can I do this using batch script?你能告诉我如何使用批处理脚本来做到这一点吗?

Input:输入:

ABCDEF  GHIJK0000ADS
ABCDEF  GHIJK0000ADS
ABCDEF  GHIJK0000ADS

Output:输出:

ABCDEF  GHIJK0000ADSABCDEF  GHIJK0000ADSABCDEF  GHIJK0000ADS

Thanks, Niranjan谢谢,尼兰詹

There is no trivial pure batch solution if you have existing lines that may begin with spaces.如果您有可能以空格开头的现有行,则没有简单的纯批处理解决方案。 It is possible to write such lines without newlines , but it takes a lot of code.可以在没有换行符的情况下编写此类行,但需要大量代码。

There are other issues that can further complicate a pure batch solution.还有其他问题会使纯批处理解决方案进一步复杂化。

In general, Windows batch is a poor choice for manipulating text files if you want a robust, general purpose solution,一般来说,如果您想要一个强大的通用解决方案,Windows 批处理是操作文本文件的糟糕选择,

That is why I wrote JREPL.BAT - a regular expression text processing utility .这就是我编写JREPL.BAT 的原因——一个正则表达式文本处理实用程序 JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. JREPL 是纯脚本(混合批处理/JScript),可以在 XP 以后的任何 Windows 机器上本地运行。 No 3rd party exe file is required.不需要第 3 方 exe 文件。

Full documentation is accessed from the command console via jrepl /?通过jrepl /?从命令控制台访问完整文档jrepl /? , or jrepl /?? , 或jrepl /?? for paged output.用于分页输出。

The solution is downright trivial with JREPL. JREPL 的解决方案非常简单。

call jrepl "[\r\n]" "" /m /f "input.txt" /o "output.txt"

If you want to overwrite the original file, then如果你想覆盖原始文件,那么

call jrepl "[\r\n]" "" /m /f "input.txt" /o -

This solution will work as long as your entire file can be read into memory by JScript.只要 JScript 可以将您的整个文件读入内存,此解决方案就会起作用。 I believe the limit is close to 1 gigabyte.我相信限制接近 1 GB。

Update 2020-07-14更新 2020-07-14

The size limit has been eliminated starting with JREPL version 8.5 that was released 2020-02-29.从 2020 年 2 月 29 日发布的 JREPL 8.5 版开始,大小限制已被取消。 Prior versions required the /M option to load the entire file into memory.以前的版本需要/M选项才能将整个文件加载到内存中。 Version 8.5 introduces the /EOL option that specifies the end of line sequence to be used when writing each line. 8.5 版引入了/EOL选项,用于指定写入每行时要使用的行尾序列。 The value can be set to an empty string, thus removing all carriage returns and line feeds, and it does this by processing one line at a time.该值可以设置为空字符串,从而删除所有回车和换行符,它通过一次处理一行来实现。

call jrepl "^" "" /eol "" /f "input.txt" /o "output.txt"
setlocal enabledelayedexpansion
set "line="
for /f "delims=" %%a in (filename.txt) do set "line=!line!%%a"
echo %line%

Read each line;accumulate.阅读每一行;积累。 Relies on delayed expansion mode依赖delayed expansion模式

Here is an alternative method:这是一种替代方法:

@echo off
for /F usebackq^ delims^=^ eol^= %%L in ("filename.txt") do (
    < nul set /P ="%%L"
)
echo/

Remove the echo/ command in case you do not want a final trailing line-break.如果您不想要最后的尾随换行符,请删除echo/命令。

Advantages:优点:

  • no accumulation of lines in a single variable, so files longer than ~ 8190 bytes are possible;单个变量中没有行的累积,因此文件长度超过 ~ 8190 字节是可能的;

Disadvantages:缺点:

  • leading white-spaces get lost;领先的空白会丢失;
  • lines must not begin with = ;行不能以=开头;

User dbenham mentioned non-trivial pure batch solutions in his answer that maintain leading white-spaces.用户dbenham他的回答中提到了保持领先空白的非平凡纯批处理解决方案。 I played around with the relying technique and come along with the following script to share:我玩弄了依赖技术,并提供了以下脚本来分享:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_INFILE=filename.txt"         & rem // (input file; `%~1` is argument)
set "_TMPNAME=%TEMP%%~n0_%RANDOM%" & rem // (name of temporary files, no ext.)

rem // Build full names of temporary files:
set "$TMPFILE=%_TMPNAME%.tmp"
set "$SUBFILE=%_TMPNAME%.sub"

rem // Store SUB (EOF) character in variable:
> nul copy nul "%$SUBFILE%" /A
for /F "usebackq" %%F in ("%$SUBFILE%") do set "$SUBCHAR=%%F"

rem // Loop through lines of input file:
for /F usebackq^ delims^=^ eol^= %%L in ("%_INFILE%") do (
    rem // Append SUB char. to current line and write to temp. file:
    > "%$SUBFILE%" echo(%%L%$SUBCHAR%
    rem // Copy temp. file to another temp. file, omitting SUB char. plus next:
    > nul copy "%$SUBFILE%" /A "%$TMPFILE%" /B
    rem // Output content of second temporary file:
    type "%$TMPFILE%"
)

rem // Clean up temporary files:
del "%$SUBFILE%" "%$TMPFILE%"

endlocal
exit /B

Besides the fact that leading white-spaces are no longer lost, this approach does not result in an error when a line begins with an = sign.除了不再丢失前导空格这一事实之外,当一行以=符号开始时,这种方法不会导致错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理:使用文件路径从文本文件末尾删除换行符 - Batch: delete line feed from end of text file using filepaths 使用批处理脚本解析文本文件并从每行中删除前 2 个字符 - Parse a text file using batch script and remove the first 2 characters from each line 回车+换行=&gt;换行 - Carriage return + Linefeed => Line feed 如何使用批处理命令从文本文件中删除 CRLF 和新行 - How to remove CRLF and a new line from text file using batch command 如何使用 Windows 中的批处理文件从文本文件的第一行中删除前导空格? - How to remove leading whitespace from first line of text files using batch file in Windows? 如何在Windows批处理文件中包含带有多行var的回车符? - How to include a carriage return character with a multiple line var in a windows batch file? 使用批处理脚本从文本文件中删除多行字符串 - Remove multi-line strings from a text file using a batch script 如何使用命令提示符或批处理文件从字符串中删除某些字符? - How to remove certain characters from a string using command prompt or batch file? 如何逐行读取批处理文件中多余字符的文本文件? 允许限制行长。(Windows,批处理脚本) - How to read text file line by line which is excessing characters in batch file? Limiting the line length is allowed.(Windows, batch script) 使用批处理删除文件的最后n个字符 - remove last n characters of a file using batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM