文本文件格式和对齐方式

时间:2013-10-25 11:07:00

标签: 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
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

EDIT1

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

3 个答案:

答案 0 :(得分:6)

尝试:

@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

答案 1 :(得分:2)

@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

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

答案 2 :(得分:0)

从GNU utils安装awk,然后使用它(假设你的信息在文件“a”中):

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