问题
I have two text files using which i want to get output.txt as follows:
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
I am trying in VBScript to achieve it :Emailing preceded by text files consolidation into excel
How i can get output.txt above using batch script ?
EDIT1
when i am using below scenario,output.txt is not coming in correct and formal order, pls see as below: FreePhysicalMemory is being misplaced:
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
回答1:
@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
回答2:
this might work for you:
@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
Output:
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
来源:https://stackoverflow.com/questions/19532889/text-files-formatting-cum-consolidation