[英]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。
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:优点:
Disadvantages:缺点:
=
;=
开头; 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.