-2

I want to remove all spaces and tabs from a txt file. Below is my code with which I am trying to do this:

for /F "delims=" %%a in (CODE_CHECK.txt) do (
   set one=%%a
   set one=%one:=%
) >> CODE_CHECK_2.txt

Example file lines:

ONE VIEW

TWO PACKEGE BODY

After the code it should be:

ONEVIEW

TWOPACKEGEBODY
Compo
  • 36,585
  • 5
  • 27
  • 39
Raitis Lebedevs
  • 47
  • 1
  • 13
  • You have two problems. One, the code is not outputting the changed variable to the output file. Two, you need to use delayed expansion. – Squashman Feb 26 '19 at 08:27
  • cat CODE_CHECK.txt | sed 's/[[:space:]]//g' >> CODE_CHECK_2.txt This worked for me! – Raitis Lebedevs Feb 26 '19 at 08:31
  • 1
    @RaitisLebedevs huh? this is windows batch file, not bash... how did you mix up these? Voting to close this topic as unclear. – Gerhard Feb 26 '19 at 08:41
  • 1
    Really everything is better than Windows __command__ processor for deleting all normal spaces and horizontal tabs from a text file. So I recommend thinking about usage of preinstalled VBScript, JScript, PowerShell, or alternatives like Python, Perl or any text editor. However, if you want to do this nevertheless with the application least suitable for this task, you need [delayed expansion](https://ss64.com/nt/delayedexpansion.html) enabled after `set "one=%%a"`, used next on `set "one=!one: =!"` and `set "one=!one:TAB=!"` and `echo(!one!` and then disabled next also in the loop. – Mofi Feb 26 '19 at 09:19
  • 2
    Read [this answer](https://stackoverflow.com/a/38676582/3074564) for details about the commands __SETLOCAL__ and __ENDLOCAL__ needed to enable and disable delayed expansion. Enabling it just once with `setlocal EnableDelayedExpansion` before the __FOR__ loop and disabling it only once with `endlocal` after the __FOR__ loop results in corrupting lines in text file containing on or more `!` on line `set "one=%%a"`. Also your __FOR__ loop ignores empty lines completely and lines starting with `;`. – Mofi Feb 26 '19 at 09:24
  • @Mofi see comment by op, seems he might be confused here with something because he ended up using `sed` – Gerhard Feb 26 '19 at 09:52

2 Answers2

1

Assuming you're on a Windows platform, due to your provided code and included tags…

The folowing example will remove spaces and tabs and also remove any blank lines:

@If Exist "CODE_CHECK.txt" (For /F Delims^=^ EOL^= %%A In ('More /T1 "CODE_CHECK.txt"')Do @Set "$=%%A"&Call Echo(%%$: =%%)>"CODE_CHECK_2.txt"

To maintain any blank lines, using a , you'd need something more like this:

@If Exist "CODE_CHECK.txt" (For /F "Tokens=1*Delims=]" %%A In ('More "CODE_CHECK.txt"^|Find /V /N ""')Do @Set "$= %%B"&Call Echo(%%$: =%%)>"CODE_CHECK_2.txt"

In this example I've removed the /T1 option for More, I'm unsure if its inclusion is more or less efficient


You could also use for this, (if needed the input and output file can be the same):

(GC 'CODE_CHECK.txt') -Replace '\s',''|SC 'CODE_CHECK_2.txt'

You can also run the version from a :

@PowerShell -NoP "(GC 'CODE_CHECK.txt') -Replace ' |\t',''|SC 'CODE_CHECK_2.txt'"

In this version, I've used ' |\t', as a possible alternative to '\s'.

Compo
  • 36,585
  • 5
  • 27
  • 39
1

Given the file holds less than 64K lines with a length less than 8K bytes/characters each, empty lines do not need to be preserved, and the file is ASCII/ANSI-encoded with DOS/Windows-style line-breaks, you could to the following:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Write output to another file:
> "CODE_CHECK_2.txt" (
    rem /* Read file by `more` which replaces TABs by SPACEs;
    rem    then parse the output by `for /F` (skipping empty lines): */
    for /F delims^=^ eol^= %%L in ('more "CODE_CHECK.txt"') do (
        rem // Store current (non-empty) line:
        set "LINE=%%L"
        rem /* Toggle delayed expansion to be able to write and read
        rem    a variable in the same block of code and to preserve `!`: */
        setlocal EnableDelayedExpansion
        rem // Replace every SPACE by nothing, hence remove it:
        echo(!LINE: =!
        endlocal
    )
)
endlocal
exit /B

Here is an approach that preserves empty lines (the remaining restrictions still apply though):

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Write output to another file:
> "CODE_CHECK_2.txt" (
    rem /* Read file by `more` which replaces TABs by SPACEs;
    rem    precede every line by line number plus `:` to not appear empty;
    rem    then parse the output by `for /F` (which would skip empty lines): */
    for /F "delims=" %%L in ('more "CODE_CHECK.txt" ^| findstr /N "^"') do (
        rem // Store current line:
        set "LINE=%%L"
        rem /* Toggle delayed expansion to be able to write and read
        rem    a variable in the same block of code and to preserve `!`: */
        setlocal EnableDelayedExpansion
        rem // Replace every SPACE by nothing, hence remove it:
        set "LINE=!LINE: =!"
        rem // Remove line number prefix and return result:
        echo(!LINE:*:=!
        endlocal
    )
)
endlocal
exit /B
aschipfl
  • 33,626
  • 12
  • 54
  • 99