I have written a simple batch file to parse a log file and print out the required data on the screen. I have everything working except for the fact that my output is not aligned properly as I would like it to be. Here is my batch script:
@echo off
setlocal
echo Name Reg No.
echo -------------------------------------
for /f "tokens=1-3 delims=' " %%A in ('findstr "Registered" registration.log') do (
echo %%B %%C
)
endlocal
The output is:
Name Reg No.
-------------------------------------
Russel McDonald 28068906
Pete 23985688
David Antony 87681747
Jaques 71979798
Jayson Burgers 21343854
Allison Jameson 87446435
But I want the output to be:
Name Reg No.
-------------------------------------
Russel McDonald 28068906
Pete 23985688
David Antony 87681747
Jaques 71979798
Jayson Burgers 21343854
Allison Jameson 87446435
How can this be done?
This batch code aligns the registration number on each output line.
@echo off
setlocal EnableDelayedExpansion
echo Name Reg No.
echo -------------------------------------
for /f "tokens=2,3 delims=' " %%A in ('%SystemRoot%\System32\findstr.exe "Registered" registration.log') do (
set "RegistrationName=%%A "
echo !RegistrationName:~0,19!%%B
)
endlocal
I suppose that file registration.log
is a tab delimited CSV file and therefore the character after delims='
is a horizontal tab character.
The method used to align registration number is appending to name 19 spaces and then output just first 19 characters from name using a substring definition.
One more version without using findstr
:
@echo off
setlocal EnableDelayedExpansion
echo Name Reg No.
echo -------------------------------------
for /f "tokens=1-3 delims=' " %%A in (registration.log) do (
if /I "%%A" == "Registered" (
set "RegistrationName=%%B "
echo !RegistrationName:~0,19!%%C
)
)
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
findstr /?
for /?
if /?
set /?
setlocal /?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.