批处理脚本FOR循环更改文件

时间:2013-09-25 15:55:32

标签: windows batch-file cmd

我有一个看起来像这样的文件:

Disk:Bus 0 Enclosure 7 Disk 14.
State:Enabled
Raw Capacity:402.61GB
User Capacity:214.29GB
LUN ID:18
LUN Type:RAID 5
Hot Spare Replacing:N/A
Firmware:C3A8
Vendor:HITACHI 
Model:HUS15604 CLAR450
Bind Signature:0xcdc0
CLARiiON TLA Part Number:005049032
Drive Type:FC
Current Speed:4Gbps
Maximum Speed:4Gbps


Disk:Bus 0 Enclosure 7 Disk 13.
State:Enabled
Raw Capacity:402.61GB
User Capacity:214.29GB
LUN ID:18
LUN Type:RAID 5
Hot Spare Replacing:N/A
Firmware:C3A8
Vendor:HITACHI 
Model:HUS15604 CLAR450
Bind Signature:0xcdc0
CLARiiON TLA Part Number:005049032
Drive Type:FC
Current Speed:4Gbps
Maximum Speed:4Gbps


Disk:Bus 0 Enclosure 7 Disk 12.
State:Enabled
Raw Capacity:402.61GB
User Capacity:214.29GB
LUN ID:18
LUN Type:RAID 5
Hot Spare Replacing:N/A
Firmware:C3A8
Vendor:HITACHI 
Model:HUS15604 CLAR450
Bind Signature:0xcdc0
CLARiiON TLA Part Number:005049032
Drive Type:FC
Current Speed:4Gbps
Maximum Speed:4Gbps

我想阅读文件并写信看起来像这样:

Disk            State     Raw_Capacity  User_Capacity
Bus 0 Enclosure 7 Disk 14.  Enabled 402.61GB              214.29GB
Bus 0 Enclosure 7 Disk 13.  Enabled 402.61GB              214.29GB
Bus 0 Enclosure 7 Disk 12.  Enabled 402.61GB              214.29GB

尝试使用FOR循环。感谢任何帮助。谢谢。

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

@echo off &setlocal enabledelayedexpansion
for /f "delims=" %%a in (file) do (
    set "line=%%a"
    if /i "!line:Disk:=!" neq "!line!" <nul set/p"=!line:*Disk:=!   "
    if /i "!line:Raw=!" neq "!line!" <nul set/p"=!line:*Capacity:=! "
    if /i "!line:User=!" neq "!line!" <nul set/p"=!line:*Capacity:=!"
    if /i "!line:Maximum Speed=!" neq "!line!" echo(
)

输出是:

Bus 0 Enclosure 7 Disk 14.      402.61GB        214.29GB
Bus 0 Enclosure 7 Disk 13.      402.61GB        214.29GB
Bus 0 Enclosure 7 Disk 12.      402.61GB        214.29GB

答案 1 :(得分:1)

file.log是此处的输入文件。我使用了Endoro的技术,但有一些小的变化。

@echo off
setlocal enabledelayedexpansion
(
echo Disk            State     Raw_Capacity  User_Capacity
for /f "usebackq delims=" %%a in ("file.log") do (
    set "line=%%a"
    if /i "!line:Disk:=!" neq "!line!" <nul set/p"=!line:*Disk:=!   "
    if /i "!line:State=!" neq "!line!" <nul set/p"=!line:*State:=!   "
    if /i "!line:Raw=!"   neq "!line!" <nul set/p"=!line:*Capacity:=! "
    if /i "!line:User=!"  neq "!line!" <nul set/p"=!line:*Capacity:=!"&echo(
)
)>"output file.txt"

答案 2 :(得分:1)

主题的另一个变体:)

@echo off
setlocal enableDelayedExpansion
>output.txt (
  echo Disk            State     Raw_Capacity  User_Capacity
  for /f "tokens=1* delims=:" %%A in (input.txt) do (
    for %%V in (Disk State "Raw Capacity") do if %%~V==%%A set "%%A=%%B"
    if "%%A"=="User Capacity" echo !Disk!  !State! !Raw Capacity!              %%B
  )
)
相关问题