从命令中选择字符并替换其中的一些
Selecting characters from command and replacing some of them
我从 NetApp 查询中得到了这个结果
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE1
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 0
storePool_ClientAlloc 60
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 0
storePool_OpenAlloc 86
storePool_OpenStateAlloc 86
storePool_OwnerAlloc 10
storePool_StringAlloc 70
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE2
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 1246
storePool_ClientAlloc 29
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 468
storePool_OpenAlloc 811
storePool_OpenStateAlloc 811
storePool_OwnerAlloc 519
storePool_StringAlloc 548
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE3
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 165
storePool_ClientAlloc 27
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 135
storePool_OpenAlloc 272
storePool_OpenStateAlloc 272
storePool_OwnerAlloc 152
storePool_StringAlloc 179
我想将其转换为类似的东西:
NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;............NODEn.storePool_ByteLockAlloc=165;
对于我从输出中获得的所有节点(不仅是 3 个)。
我试过用这个替换输出:
output.replace(' ', '').replace('Alloc', 'Alloc=').replace('\r','').replace('\n',' ')
但这并没有给我我需要的结果。
一些想法?
非常感谢您的帮助。
如果将输出捕获到一个长字符串中,则可以使用正则表达式查找感兴趣的行并将它们解析为您请求的格式:
import re
output = """
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE1
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 0
storePool_ClientAlloc 60
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
.
.
.
"""
rv = []
current_node = None
for match in re.findall(r'Scope: (NODE\d+)|(storePool_.*)', output):
node, metric = match
if node and current_node != node:
current_node = node
if current_node and metric:
name, value = metric.strip().split()
rv.append(f'{current_node.strip()}.{name}={value}')
print(';'.join(rv))
输出:
NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;NODE1.storePool_DelegAlloc=0;...
我从 NetApp 查询中得到了这个结果
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE1
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 0
storePool_ClientAlloc 60
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 0
storePool_OpenAlloc 86
storePool_OpenStateAlloc 86
storePool_OwnerAlloc 10
storePool_StringAlloc 70
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE2
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 1246
storePool_ClientAlloc 29
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 468
storePool_OpenAlloc 811
storePool_OpenStateAlloc 811
storePool_OwnerAlloc 519
storePool_StringAlloc 548
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE3
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 165
storePool_ClientAlloc 27
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
storePool_DelegStateAlloc 0
storePool_LayoutAlloc 0
storePool_LayoutStateAlloc 0
storePool_LockStateAlloc 135
storePool_OpenAlloc 272
storePool_OpenStateAlloc 272
storePool_OwnerAlloc 152
storePool_StringAlloc 179
我想将其转换为类似的东西:
NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;............NODEn.storePool_ByteLockAlloc=165;
对于我从输出中获得的所有节点(不仅是 3 个)。
我试过用这个替换输出:
output.replace(' ', '').replace('Alloc', 'Alloc=').replace('\r','').replace('\n',' ')
但这并没有给我我需要的结果。
一些想法?
非常感谢您的帮助。
如果将输出捕获到一个长字符串中,则可以使用正则表达式查找感兴趣的行并将它们解析为您请求的格式:
import re
output = """
Object: nfsv4_diag
Instance: nfs4_diag
Start-time: 6/4/2020 16:55:40
End-time: 6/4/2020 16:55:40
Scope: NODE1
Counter Value
-------------------------------- --------------------------------
storePool_ByteLockAlloc 0
storePool_ClientAlloc 60
storePool_CopyStateAlloc 0
storePool_DelegAlloc 0
.
.
.
"""
rv = []
current_node = None
for match in re.findall(r'Scope: (NODE\d+)|(storePool_.*)', output):
node, metric = match
if node and current_node != node:
current_node = node
if current_node and metric:
name, value = metric.strip().split()
rv.append(f'{current_node.strip()}.{name}={value}')
print(';'.join(rv))
输出:
NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;NODE1.storePool_DelegAlloc=0;...