79} How can I trim leading and trailing spaces [from a variable]?
If it is about a
file,
here is a demonstration
@echo off & setlocal enableextensions
:: Make and display a temporary test file
echo This is a test >MyTest.txt
echo Second line >>MyTest.txt
echo
.>>MyTest.txt
echo Fourth line >>MyTest.txt
type Mytest.txt
:: Trim
<Mytest.txt sed -e "s/^ *//" -e "s/ *\.$//"
:: Clean up
del Mytest.txt
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
·This··is··a··test···
···Second··line·····
····················
···Fourth··line·····
This is a test
Second line
Fourth line
Or, with a pure script, provided there are no backslashes (\)
@echo off & setlocal enableextensions enabledelayedexpansion
set debug=X
:: Make a temporary test file
echo This is a test >MyTest.txt
echo Second line >>MyTest.txt
echo
.>>MyTest.txt
echo Fourth line >>MyTest.txt
:: Trim
for /f "tokens=* delims= " %%a in ('type Mytest.txt') do (
set s_=%%~na
echo !s_!%debug%
)
:: Clean up
del Mytest.txt
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
This is a testX
Second lineX
X
Fourth lineX
The latter option is not without problems. Note what happens if we substitute
set debug=
The output will be
D:\TEST>cmdfaq
This is a test
Second line
ECHO is off.
Fourth line
A solution by Ralph Brown
@echo off & setlocal enableextensions
:: Make and display a temporary test file
echo This is a test >MyTest.txt
echo Second line >>MyTest.txt
echo.>>MyTest.txt
echo Fourth line >>MyTest.txt
::
rem FIND /?
rem /V Displays all lines NOT containing the specified string
rem /N Displays line numbers with the displayed lines.
for /f "tokens=1,* delims=]" %%L in ('
type MyTest.txt ^| find /v /n ""') do call :trim %%M
::
:: Clean up
del Mytest.txt
endlocal & goto :EOF
::
:trim
@echo.%*
exit/b
The output will be
D:\TEST>cmdfaq
This··is··a··test
Second··line
Fourth··line
But let's move on. If it is about an
environment variable:
@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
echo %S%|
sed -e "s/^ *//" -e "s/ *$//" -e "s/^/@set S=&/">"%temp%\tmp$$$.cmd"
for %%c in (call del) do %%c "%temp%\tmp$$$.cmd"
echo %S%.
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
This is a test .
This is a test.
The
problem can also be solved with a Visual Basic Script (VBScript)
@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
>"%temp%\tmp$$$.vbs" echo WScript.Echo Trim("%S%")
for /f "tokens=* delims=" %%a in (
'cscript //nologo "%temp%\tmp$$$.vbs"') do set S=%%a
for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
echo %S%.
endlocal & goto :EOF
The output will again be
D:\TEST>cmdfaq
This is a test .
This is a test.
Or, with a pure script (which also condenses any multiple spaces)
@echo off & setlocal enableextensions
rem enabledelayedexpansion
set S= This is a test
echo %S%.
for /f "tokens=* delims= " %%a in ('echo %S%') do set S=%%a
echo %S%.
endlocal & goto :EOF
Note, however, that the output will be a different:
D:\TEST>cmdfaq
This is a test .
This is a test .
@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
for /f "tokens=* delims= " %%a in ('echo %S%
') do set S=%%a
set S=%S:~0,-1%
echo %S%.
endlocal & goto :EOF
Now also the trailing space is omitted:
D:\TEST>cmdfaq
This is a test .
This is a test.
Likewise, one can drop the _leading_ zeros:
@echo off & setlocal enableextensions
set num=0800
for /f "tokens=* delims=0" %%a in ('echo %num%') do set num=%%a
echo %num%
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
800
How can I trim trailing spaces from a file?
First write the following Visual Basic Script file
calling it RTRIMMER.VBS
Do While Not WScript.StdIn.AtEndOfStream
WScript.Echo RTrim(WScript.StdIn.ReadLine)
Loop
Then apply
cscript //nologo TRIMMER.VBS < "My Old Text File.txt" > "My New Text File.txt"
For trimming leading spaces use
LTRIM
and for applying both use
TRIM.
Another solution is
<"My Old Text File.txt"
sed -e "s/ *$//" >"My New Text File.txt"