Reputation: 8292
I have a text file output.txt which contents would be changed everyday but format would be always like below with:
Minimum text string length= 5
Maximum text string length= 22
Maximum numeric string would be only upto 2 digit before and after decimal.
output.txt
OPERATINGSYSTEM SERVER1 SERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
I want to make the content properly alingned and formatted in column as:
OPERATINGSYSTEM SERVER1 SERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
I tried below but no output i get:
@ECHO OFF
SETLOCAL
SET "spaces= "
SET "OPERATINGSYSTEM=MacOS"
SET /a SERVER1=3.45
CALL :formatout
SET "OPERATINGSYSTEM=TotalVisibleMemorySize"
SET /a SERVER1=48.00
CALL :formatout
GOTO :EOF
:formatout
CALL :padright name 15
CALL :padleft size 11
ECHO +%OPERATINGSYSTEM %+%SERVER1%+%SERVER2%
GOTO :eof
:padright
CALL SET padded=%%%1%%%spaces%
CALL SET %1=%%padded:~0,%2%%
GOTO :eof
:padleft
CALL SET padded=%spaces%%%%1%%
CALL SET %1=%%padded:~-%2%%
GOTO :eof
EDIT1
When output.txt is as below:
OPERATINGSYSTEM PROJECTSERVER1 PROJECTSERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
CPULoadPercentage 2 4
I am trying with all the possible combination of spacelength in t1 and t2, but not getting perfectly alinged newoutput.txt file as:
OPERATINGSYSTEM PROJECTSERVER1 PROJECTSERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
CPULoadPercentage 2 4
Code i am using:
@ECHO OFF &SETLOCAL
set "t1= "
set "t2= "
(for /f "tokens=1-3" %%a in (Managed.txt) do (
set "v1=%%a%t1%"&set "v2=%t2%%%b"& set "v3=%t2%%%c"
call echo(%%v1:~0,25%%%%v2:~-14%%%%v3:~-14%%
))>newoutput.txt
Upvotes: 0
Views: 2390
Reputation: 70923
@echo off
setlocal
for /f "tokens=1-3" %%f in (output.txt) do call :formatOut %%f %%g %%h
endlocal
exit /b
:formatOut
setlocal
set "spaces= "
set col1=%~1%spaces%
set col1=%col1:~0,30%
set col2=%spaces%%~2
set col2=%col2:~-7%
set col3=%spaces%%~3
set col3=%col3:~-7%
echo %col1% %col2% %col3%
endlocal
goto :EOF
To generate file output, call as formatData.cmd > finalFile.txt
Upvotes: 2
Reputation: 37569
try:
@ECHO OFF &SETLOCAL
set "t1= "
set "t2= "
(for /f "tokens=1-3" %%a in (output.txt) do (
set "v1=%%a%t1%"&set "v2=%t2%%%b"& set "v3=%t2%%%c"
call echo(%%v1:~0,25%%%%v2:~-8%%%%v3:~-8%%
))>out.txt
out.txt
:
OPERATINGSYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux12 2.78 5.76 MacOS 3.45 6.39 Ubuntu 4.12 0.00 Android 0.00 3.46 FreePhysicalMemory 30.12 31.65 TotalVisibleMemorySize 48.00 48.00
Upvotes: 6
Reputation: 207425
Install awk from GNU utils, and use this (assuming your info is in file "a"):
awk '{printf("%-24s %12s %12s\n",$1,$2,$3)}' a
Upvotes: 0