join多个文本文件

我有四个文本文件,如附图所示。 文件 - 图片

我用来join和巩固它们的广义脚本是:

@echo off setlocal enableDelayedExpansion :: Clear any existing $ variables for /f "delims==" %%A in ('"set $ 2>nul"') do set "%%A=" :: Load data set "cnt=0" set "blank= " set "null= NULL" set "width=8" set "labelWidth=22" set "lineWidth=0" set "lookup= FreePhysicalMemory:2 TotalVisibleMemorySize:3 CPULoadPercentage:4" for %%F in (server1.txt server2.txt server3.txt server4.txt) do ( set "val=%blank%%%~nF" set "$0.OPERATINGSYSTEM=!$0.OPERATINGSYSTEM! !val:~-%width%!" for /f "tokens=1,2" %%A in (%%F) do ( set "test=!lookup:* %%A:=!" if "!test!" equ "!lookup!" (set "prefix=$1.") else set "prefix=$!test:~0,1!." for %%P in (!prefix!) do ( if not defined !prefix!%%A ( for /l %%N in (1 1 !cnt!) do set "%%P%%A=!%%P%%A! !null:~-%width%!" ) else if "!%%A!" neq "!cnt!" ( set "%%P%%A=!%%P%%A! !null:~-%width%!" ) set "val=%blank%%%B" set "%%P%%A=!%%P%%A! !val:~-%width%!" ) set /a "%%A=cnt+1" ) set /a cnt+=1, lineWidth+=width+1 ) :: Print results (for /f "tokens=2* delims=.=" %%A in ('set $') do ( set "line=%%B !null:~-%width%!" set "label=%%A%blank%" echo !label:~0,%labelWidth%!!line:~0,%lineWidth%! ))>output.txt 

而我在output.txt中得到的结果是:

 OPERATINGSYSTEM server1 server2 server3 server4 Android 1.262 2.262 3.262 4.636 DOS 1.253 NULL 4.253 NULL MacOS NULL NULL NULL 4.111 Ubuntu 1.674 NULL Windows 1.111 2.151 3.636 4.453 FreePhysicalMemory 11.493 22.492 33.491 44.494 TotalVisibleMemorySize 11.988 22.988 33.988 44.988 CPULoadPercentage 1 2 3 4 

现在我们可以看到在output.txt文件中遇到以下两个问题:

  1. 针对ubuntu,NULL只会来自server2,而server3和server4也会来。

  2. 对于DOS,值4.253在server3下,NULL在server4下,实际值4.253在server4下,NULL在server3下。

rest似乎是正确和好的。

你能否提出一个build议,为什么这个脚本在这里显示的不寻常的行为或者其他任何方式来实现呢?

Solutions Collecting From Web of "join多个文本文件"

对于任何感兴趣的人来说,这个问题是文本输出位置的后续,代码是从我更新的答案到这个问题 。

我的代码有一些严重的错误,这些错误都在下面解决。 我认为现在的代码实际上更简单了。

 @echo off setlocal enableDelayedExpansion :: Clear any existing $ variables for /f "delims==" %%A in ('"set $ 2>nul"') do set "%%A=" :: Define column widths set "width=8" set "labelWidth=22" :: Define summary rows and their order, must start order at 2 and cannot exceed 9 set "lookup= FreePhysicalMemory:2 TotalVisibleMemorySize:3 CPULoadPercentage:4" :: Initialize some needed values set "blank= " set "null=%blank%NULL" set "null=!null:~-%width%!" set "nulls=" set "lineWidth=0" :: Load data for %%F in (server1.txt server2.txt server3.txt server4.txt) do ( set "val=%blank%%%~nF" set "$0.OPERATINGSYSTEM=!$0.OPERATINGSYSTEM! !val:~-%width%!" for /f "tokens=1,2" %%A in (%%F) do ( set "test=!lookup:* %%A:=!" if "!test!" equ "!lookup!" (set "prefix=$1.") else set "prefix=$!test:~0,1!." for %%P in (!prefix!) do for %%L in (!lineWidth!) do ( set "%%P%%A=!%%P%%A!!nulls!" if defined %%P%%A set "%%P%%A=!%%P%%A:~0,%%L!" set "val=%blank%%%B" set "%%P%%A=!%%P%%A! !val:~-%width%!" ) ) set /a lineWidth+=width+1 set "nulls=!nulls! !null!" ) :: Print results (for /f "tokens=2* delims=.=" %%A in ('set $') do ( set "line=%%B!nulls!" set "label=%%A%blank%" echo !label:~0,%labelWidth%!!line:~0,%lineWidth%! ))>output.txt