加入多个文本文件

时间:2014-03-26 07:28:55

标签: windows batch-file for-loop text-files

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

我用来加入和合并它们的通用脚本是:

@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文件中遇到以下2个问题:

  1. 对于ubuntu,只有server2才会出现NULL 对于server3和server4也是如此。

  2. 对于DOS,值4.253将在server3下,NULL即将到来     在server4下,而实际值4.253应该在server4下     和NULL应该在server3下。

  3. 休息似乎是正确和良好的。

    请问您为什么这个脚本在这里显示的异常行为或任何其他方式来实现相同的目的?

1 个答案:

答案 0 :(得分:2)

对于有兴趣的人,此问题是对Text output placement的跟进,代码来自my updated answer to that question

我的代码有一些严重的错误,这些错误已在下面修复。我认为代码现在实际上更简单。

@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