问题
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
回答1:
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 poor choice for manipulating text files if you want a robust, general purpose solution,
That is why I wrote JREPL.BAT - a regular expression text processing utility. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe file is required.
Full documentation is accessed from the command console via jrepl /?
, or jrepl /??
for paged output.
The solution is downright trivial with 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. I believe the limit is close to 1 gigabyte.
回答2:
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
回答3:
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.
Advantages:
- no accumulation of lines in a single variable, so files longer than ~ 8190 bytes are possible;
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. 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.
来源:https://stackoverflow.com/questions/40480974/how-to-remove-carriage-return-and-line-feed-characters-from-a-text-file-using-ba