1

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

Magoo
  • 77,302
  • 8
  • 62
  • 84
Niranjan Reddy
  • 11
  • 1
  • 1
  • 2

3 Answers3

4

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,

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.

Update 2020-07-14

The size limit has been eliminated starting with JREPL version 8.5 that was released 2020-02-29. Prior versions required the /M option to load the entire file into memory. Version 8.5 introduces the /EOL option that specifies the end of line sequence to be used when writing each line. 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"
dbenham
  • 127,446
  • 28
  • 251
  • 390
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

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I'm not sure that it is OK to have \r\n at the end of the file. Also, you are making an assumption that the total file length is less than 8191 bytes long. True for the example in the question, but I doubt that is the actual data. – dbenham Nov 08 '16 at 15:59
1

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.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • It is not working .Could you tell me what should be passed to parameter %%L? – Niranjan Reddy Nov 08 '16 at 13:58
  • What do you mean by "not working"? It perfectly works for me, using your sample input data (stored in file `filename.txt` in the current working directory)... – aschipfl Nov 08 '16 at 15:15