我有两个文本文件,我想使用它来获取output.txt,如下所示:
FILE1.TXT:
Windows 1.36
Linux 2.78
MacOS 3.45
Ubuntu 4.12
FreePhysicalMemory 30.12
TotalVisibleMemorySize 48.00
FILE2.TXT:
MacOS 6.39
Windows 4.42
Linux 5.76
Android 3.46
FreePhysicalMemory 31.65
TotalVisibleMemorySize 48.00
output.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来实现它:Emailing preceded by text files consolidation into excel
如何使用批处理脚本获取上面的output.txt?
EDIT1
当我使用以下方案时, output.txt 未按正确顺序排列,请参阅以下内容: FreePhysicalMemory错位:
FILE1.TXT:
Windows 1.36
Linux 2.78
MacOS 3.45
Ubuntu 4.12
FreePhysicalMemory 30.12
TotalVisibleMemorySize 48.00
CPULoadPercentage 2
FILE2.TXT:
MacOS 6.39
Windows 4.42
Linux 5.76
Android 3.46
FreePhysicalMemory 31.65
TotalVisibleMemorySize 48.00
CPULoadPercentage 4
output.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
答案 0 :(得分: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
Output.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
答案 1 :(得分: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
输出:
OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76 MacOS 3.45 6.39 Android 0.00 3.46 Ubuntu 4.12 0.00 FreePhysicalMemory 30.12 31.65 TotalVisibleMemorySize 48.00 48.00