1

我有两个文本文件,我想使用它们来获取 output.txt,如下所示:

文件 1.txt:

Windows                  1.36
Linux                    2.78
MacOS                    3.45
Ubuntu                   4.12
FreePhysicalMemory      30.12
TotalVisibleMemorySize  48.00

文件2.txt:

MacOS                    6.39
Windows                  4.42
Linux                    5.76
Android                  3.46
FreePhysicalMemory      31.65
TotalVisibleMemorySize  48.00

输出.txt:

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36     4.42
Linux                    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

我正在尝试在 VBScript 中实现它:电子邮件先于文本文件合并到 excel

我如何使用批处理脚本获得上面的 output.txt ?

编辑1

当我使用以下场景时,output.txt的顺序不正确和正式,请参见如下:FreePhysicalMemory 被放错了位置

文件 1.txt:

Windows                  1.36
Linux                    2.78
MacOS                    3.45
Ubuntu                   4.12
FreePhysicalMemory      30.12
TotalVisibleMemorySize  48.00
CPULoadPercentage         2 

文件2.txt:

MacOS                    6.39
Windows                  4.42
Linux                    5.76
Android                  3.46
FreePhysicalMemory      31.65
TotalVisibleMemorySize  48.00
CPULoadPercentage         4 

输出.txt:

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36       4.42
Linux                    2.78       5.76
MacOS                    3.45       6.39
Ubuntu                   4.12       0.00
FreePhysicalMemory      30.12      31.65
Android                  0.00       3.46
TotalVisibleMemorySize  48.00      48.00
CPULoadPercentage         2       4
4

2 回答 2

4
@echo off
setlocal EnableDelayedExpansion

rem Read info from file1
set i=0
for /F "tokens=1,2" %%a in (file1.txt) do (
   set /A i+=1
   set order[!i!]=%%a
   set info[%%a]=%%b
)

rem Save totals
set total2=!order[%i%]!
set /A i-=1
set total1=!order[%i%]!
set /A i-=1

rem Read/merge info from file2
for /F "tokens=1,2" %%a in (file2.txt) do (
   if defined info[%%a] (
      set info[%%a]=!info[%%a]! %%b
   ) else (
      set /A i+=1
      set order[!i!]=%%a
      set info[%%a]=0.00 %%b
   )
)

rem Return totals to end
set /A i+=1
set order[%i%]=%total1%
set /A i+=1
set order[%i%]=%total2%

rem Format and output information
(
echo OPERATING SYSTEM       SERVER1    SERVER2
for /L %%i in (1,1,%i%) do (
   for /F %%a in ("!order[%%i]!") do (
      for /F "tokens=1,2" %%b in ("!info[%%a]!") do (
         set "os=%%a                      "
         set "s1=  %%b"
         if "%%c" equ "" (
            set "s2=     0.00"
         ) else (
            set "s2=     %%c"
         )
         echo !os:~0,22!  !s1:~-5!  !s2:~-9!
      )
   )
)
) > output.txt

输出.txt:

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36       4.42
Linux                    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
于 2013-10-23T07:57:02.840 回答
3

这可能对你有用:

@ECHO OFF &SETLOCAL
set "t1=                    "
set "t2=     "
for %%i in (1 2) do for /f %%a in (file%%i.txt) do for %%j in (1 2) do set "$%%j$%%a=0.00"
for %%i in (1 2) do for /f "tokens=1,2" %%a in (file%%i.txt) do set "$%%i$%%a=%%b"
for /f "tokens=1-3delims=$=" %%a in ('set "$1$"') do (
    if "%%c"=="0.00" (call set "var=0%%$2$%%b%%") else set "var=0%%c"
    call set "$0%%var:~-5%%=%%b"
)
(echo OPERATING SYSTEM          SERVER1 SERVER2
for /f "tokens=2delims==" %%a in ('set "$0"') do (
    set "var1=%%a%t1%"
    call set "var2=%t2%%%$1$%%a%%"
    call set "var3=%t2%%%$2$%%a%%"
    call echo(%%var1:~0,25%%%%var2:~-8%%%%var3:~-8%%
))>output.txt

输出:

操作系统 SERVER1 SERVER2
视窗 1.36 4.42
Linux 2.78 5.76
macOS 3.45 6.39
安卓 0.00 3.46
Ubuntu 4.12 0.00
免费物理内存 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
于 2013-10-23T11:12:42.557 回答