gpt4 book ai didi

linux - 重新格式化输出

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:27 24 4
gpt4 key购买 nike

我正在使用以下命令获取磁盘状态:

/opt/Navisphere/bin/naviseccli -h VNX5500-1-1 getdisk -state

我得到的输出是这样的:

Bus 0 Enclosure 0  Disk 0

State: Enabled
Bus 0 Enclosure 0 Disk 2

State: Enabled
Bus 0 Enclosure 0 Disk 4

State: Enabled
Bus 0 Enclosure 0 Disk 6

State: Enabled
Bus 0 Enclosure 0 Disk 8

State: Enabled
Bus 0 Enclosure 0 Disk 10

State: Enabled
Bus 0 Enclosure 0 Disk 12

State: Enabled
Bus 0 Enclosure 0 Disk 14

State: Enabled
Bus 0 Enclosure 0 Disk 16

我正在尝试格式化输出以便为每条总线获取一条线。像这样的东西:

Bus 0 Enclosure 0  Disk 0   [State: Enabled]
Bus 0 Enclosure 0 Disk 2 [State: Enabled]
..

最佳答案

使用awk:

awk '/^Bus/ {a=$0}; /^State/ {print a" ["$1, $2"]"}' file.txt 
  • /^Bus/{a=$0}:如果行以Bus开头,将行/记录保存为变量a

  • /^State/{print a"["$1, $2"]"}:如果行以State开头,保存打印字段带有 a 的所需格式

示例:

% cat file.txt
Bus 0 Enclosure 0 Disk 0

State: Enabled
Bus 0 Enclosure 0 Disk 2

State: Enabled
Bus 0 Enclosure 0 Disk 4

State: Enabled
Bus 0 Enclosure 0 Disk 6

State: Enabled
Bus 0 Enclosure 0 Disk 8

State: Enabled
Bus 0 Enclosure 0 Disk 10

State: Enabled
Bus 0 Enclosure 0 Disk 12

State: Enabled
Bus 0 Enclosure 0 Disk 14

State: Enabled
Bus 0 Enclosure 0 Disk 16


% awk '/^Bus/ {a=$0}; /^State/ {print a" ["$1, $2"]"}' file.txt
Bus 0 Enclosure 0 Disk 0 [State: Enabled]
Bus 0 Enclosure 0 Disk 2 [State: Enabled]
Bus 0 Enclosure 0 Disk 4 [State: Enabled]
Bus 0 Enclosure 0 Disk 6 [State: Enabled]
Bus 0 Enclosure 0 Disk 8 [State: Enabled]
Bus 0 Enclosure 0 Disk 10 [State: Enabled]
Bus 0 Enclosure 0 Disk 12 [State: Enabled]
Bus 0 Enclosure 0 Disk 14 [State: Enabled]

关于linux - 重新格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com