我有两个文本文件,我想使用它们来获取 output.txt,如下所示:
文件 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:
OPERATINGSYSTEM 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
CPULoadPercentage 2 4
但这里的问题是,使用以下代码我得到了 output.txt 如下所示,其中FreePhysicalMemory 被放错了位置..:
代码:
@echo off
setlocal EnableDelayedExpansion
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
)
set total2=!order[%i%]!
set /A i-=1
set total1=!order[%i%]!
set /A i-=1
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
)
)
set /A i+=1
set order[%i%]=%total1%
set /A i+=1
set order[%i%]=%total2%
(
echo OPERATINGSYSTEM 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:
OPERATINGSYSTEM 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