我有一个文本文件 output.txt,其内容每天都会更改,但格式总是如下所示:
Minimum text string length= 5
Maximum text string length= 22
Maximum numeric string would be only upto 2 digit before and after decimal.
输出.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
我想让内容在列中正确排列和格式化为:
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
我在下面尝试过,但没有得到任何输出:
@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
编辑1
当output.txt如下:
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
我正在尝试 t1 和 t2 中空间长度的所有可能组合,但没有得到完美的newoutput.txt文件:
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
我正在使用的代码:
@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