简体   繁体   English

从命令中选择字符并替换其中的一些

[英]Selecting characters from command and replacing some of them

I have this result from a NetApp query我从 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 

And I would like to transform it to something like:我想将其转换为:

NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;............NODEn.storePool_ByteLockAlloc=165;

For all the nodes I have from the output (not only 3).对于我从 output 获得的所有节点(不仅是 3 个)。

I have tried replacing the output using this:我尝试使用以下方法替换 output:

output.replace(' ', '').replace('Alloc', 'Alloc=').replace('\r','').replace('\n',' ')

But this is not giving me the result that I need.但这并没有给我我需要的结果。

Some ideas?一些想法?

Thanks a lot in advance for your help.非常感谢您的帮助。

If you capture the output into one long string, you can use a regex to find the lines of interest and parse them into the format you're requesting:如果您将 output 捕获到一个长字符串中,您可以使用正则表达式查找感兴趣的行并将它们解析为您请求的格式:

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))

Output: Output:

NODE1.storePool_ByteLockAlloc=0;NODE1.storePool_ClientAlloc=60;NODE1.storePool_CopyStateAlloc=0;NODE1.storePool_DelegAlloc=0;...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM