Batch file 文本文件格式和对齐

Batch file 文本文件格式和对齐,batch-file,Batch File,我有一个文本文件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. output.txt 我想让内容在列中正确对齐并格式化为: OPERATINGSYSTEM SERVER1 SERVER2

我有一个文本文件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.
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
我在下面进行了尝试,但没有得到任何输出:

@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文件,如下所示:

我使用的代码:


要生成文件输出,请调用formatData.cmd>finalFile.txt

从GNU utils安装awk,并假设您的信息在文件a中使用此选项:

awk '{printf("%-24s %12s %12s\n",$1,$2,$3)}' a
尝试:

out.txt:


我可以在哪里添加我的输入文本文件..?对于output.txt中的/f tokens=1,2,3%%f,请调用:formattout%%f%%g%%houtput重定向不起作用..我正在将'>Newoutput.txt'放在'echo%col1%%col2%%col3%'之后。对每行输出都执行echo行。使用>作为重定向程序,在每次执行时覆盖文件。在output.txt中使用/f tokens=1,2,3%%f执行调用:formatOut%%f%%g%%h>>newOutput.txt或将整个批处理文件重定向为formatData.cmd>newOutput.txtI可能见过。有问题的行是针对….>>newOutput.txt如果批处理运行两次,并且在第二次运行之前未删除输出文件,则新生成的内容将附加到现有文件。在运行之前删除输出文件,或使用>更改>>或使用相应的Proposed方法,这三种方法中的任何一种都可以解决此问题。我从没想过用这么小的代码就可以完成。工作很好。谢谢@恩多..请参见问题中的EDIT1..:@Sunny Pls。请看我的.Mark..AWK真是太棒了..但我必须在我受限制的windows上安装它的二进制文件。
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
@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
awk '{printf("%-24s %12s %12s\n",$1,$2,$3)}' a
@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
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