Question

I have four text files as shown in attached image.FILES-Image

generalized Script which I'm using to join and consolidate them is:

@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

and the result I'm getting in output.txt is:

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

now as we can see there are following 2 issues encountered in above output.txt file:

  1. Against ubuntu ,NULL is coming only for server2 while it should come for server3 and server4 as well.

  2. Against DOS ,value 4.253 is coming under server3 and NULL is coming under server4 while actually value 4.253 should come under server4 and NULL should come under server3.

Rest seems to be correct and good.

Could you please suggest why this unusual behavior this script is showing here or any other way to achieve the same ?

Was it helpful?

Solution

For anyone interested, this question is a follow up to Text output placement, and the code is from my updated answer to that question.

My code had some serious bugs, which have all been fixed below. I think the code is now actually simpler.

@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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top