的多个文本文件加入 [英] Joining of multiple text files

查看:384
本文介绍了的多个文本文件加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个文本文件,如图附加的图像。

I have four text files as shown in attached 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的,NULL只能来找当Server2上它应
    对于服务器3和服务器4也是如此。

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

针对DOS,价值4.253是根据服务器3来了,NULL来了
    在服务器4,而实际上的价值4.253应该在服务器4
    和NULL应该在服务器3。

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 ?

推荐答案

有关任何有兴趣,这个问题是一个跟进文本输出安置和code是我更新,回答这个问题

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

我的code有一些严重的错误,这已经全部低于固定。我认为,code是现在实际上是简单的。

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

这篇关于的多个文本文件加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆