文本文件格式暨盘整 [英] Text files formatting-cum-consolidation

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

问题描述

我使用的我想要得到output.txt的两个文本文件如下:

FILE1.TXT:

  1.36的Windows
Linux的2.78
MacOS的3.45
Ubuntu的4.12
FreePhysicalMemory 30.12
TotalVisibleMemorySize 48.00

FILE2.TXT:

  6.39的MacOS
4.42的Windows
Linux的5.76
Android版3.46
FreePhysicalMemory 31.65
TotalVisibleMemorySize 48.00

output.txt的:

 操作系统SERVER1 SERVER2
视窗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

我想在VBScript来实现它:<一href=\"http://stackoverflow.com/questions/19497139/emailing-$p$pceded-by-text-files-consolidation-into-excel\">Emailing $ P $通过文本文件合并pceded到Excel

我怎样才能output.txt的上述使用批处理脚本?

EDIT1

当我使用下面的场景

output.txt的在正确的正式订单不来,请参阅如下: FreePhysicalMemory被放错了地方

FILE1.TXT:

  1.36的Windows
Linux的2.78
MacOS的3.45
Ubuntu的4.12
FreePhysicalMemory 30.12
TotalVisibleMemorySize 48.00
CPULoadPercentage 2

FILE2.TXT:

  6.39的MacOS
4.42的Windows
Linux的5.76
Android版3.46
FreePhysicalMemory 31.65
TotalVisibleMemorySize 48.00
CPULoadPercentage 4

output.txt的:

 操作系统SERVER1 SERVER2
视窗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


解决方案

 关闭@echo
SETLOCAL EnableDelayedExpansion从文件1 REM读取信息
集I = 0
FOR / F令牌= 1,2%%一中(FILE1.TXT)做(
   集/ A I + = 1
   为了集[!我!] = %%一
   集信息[%% A] = %% b
)REM保存总计
一套共2 =!为了[%I%]!
集/ A的I = 1
一套共1 =!为了[%I%]!
集/ A的I = 1REM读/合并来自file2的信息
FOR / F令牌= 1,2%%一中(FILE2.TXT)做(
   如果定义信息[%% A](
      集信息[%% A] =!资讯[%% A]! %% b
   )其他(
      集/ A I + = 1
      为了集[!我!] = %%一
      设置信息[%% A] = 0.00 %% b
   )
)REM返回合计结束
集/ A I + = 1
为了集[%I%] =%共1页%
集/ A I + = 1
为了集[%I%] =%共2%REM格式和输出信息

回声操作SYSTEM SERVER1 SERVER2
对/ L %%我在(1,1,%I%),做(
   FOR / F %%一中(!为了[%% I]!)做(
      FOR / F令牌= 1,2中%% B(!信息[%% A]!)做(
         设置OS = %%一个
         设置S1 = %% B
         如果%% CEQU(
            设置S2 = 0.00
         )其他(
            设置S2 = %% C
         )
         回声OS:0.22〜! !S1:-5〜! !S2:〜-9!
      )
   )

)&GT; output.txt的

Output.txt的:

 操作系统SERVER1 SERVER2
视窗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 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

解决方案

@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

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

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