帮助我获取命令或 ansibile 正则表达式来获取以下详细信息

帮助我获取命令或 ansibile 正则表达式来获取以下详细信息

我的数据:

KVM05786Y: The Communication host "ADMK05786HTREDIRECT_JA3A" is SUCESS
KVM05786Y: The Communication host "KOPYUTRCEPORT_JA3A" is SUCESS
KVM05786Y: The Communication host "ADMK05786HTSRM_JA3A" is SUCESS
KVM05786Y: The Communication host "ADMK05786HTIAS_JA3A" is SUCESS
KVM05786Y: The Communication host "ADMK05786HTOTN_JA3A" is SUCESS
KVM05786Y: The Communication host "ADMK05786HTWPM" is SUCESS
KVM05786Y: The Communication host "OCAIDEKD_JA3A" is SUCESS
KVM05786Y: The Communication host "ADMK05786HTSC_JA3A" is SUCESS

从上面的数据中,我只想显示下面的名称。这意味着获取 afterhost和 before 之前找到的文本is。请给我一个可以做到这一点的 Linux 命令。

ADMK05786HTREDIRECT_JA3A
KOPYUTRCEPORT_JA3A
ADMK05786HTSRM_JA3A
ADMK05786HTIAS_JA3A
ADMK05786HTOTN_JA3A
ADMK05786HTWPM
OCAIDEKD_JA3A
ADMK05786HTSC_JA3A

我在ansible中尝试过:

name: "Get data"
shell: cat /var/tmp/http_data.txt
register: key_data
set_fact:
service_name: "{{ service_name | default([]) + [item|regex_search( 'The Communication host\s+"(\S+)"\s+is SUCESS.*','\1' )] }}"

with_items: "{{ key_data.stdout_lines }}"

答案1

几种不同的方法:

  1. 选择host "和之间的任何内容" is

    $ sed -En 's/.*host "([^"]*)" is.*/\1/p' file
    ADMK05786HTREDIRECT_JA3A
    KOPYUTRCEPORT_JA3A
    ADMK05786HTSRM_JA3A
    ADMK05786HTIAS_JA3A
    ADMK05786HTOTN_JA3A
    ADMK05786HTWPM
    OCAIDEKD_JA3A
    ADMK05786HTSC_JA3A
    

    或者,使用支持grepperl 兼容的正则表达式 ( -P)环顾四周:

    $ grep -oP '(?<=host ").+?(?=" is )' file
    ADMK05786HTREDIRECT_JA3A
    KOPYUTRCEPORT_JA3A
    ADMK05786HTSRM_JA3A
    ADMK05786HTIAS_JA3A
    ADMK05786HTOTN_JA3A
    ADMK05786HTWPM
    OCAIDEKD_JA3A
    ADMK05786HTSC_JA3A
    
  2. 使用队列中的位置;我们假设您始终需要第五个空格分隔字段:

    $ awk '{print $5}' file | tr -d '"'
    ADMK05786HTREDIRECT_JA3A
    KOPYUTRCEPORT_JA3A
    ADMK05786HTSRM_JA3A
    ADMK05786HTIAS_JA3A
    ADMK05786HTOTN_JA3A
    ADMK05786HTWPM
    OCAIDEKD_JA3A
    ADMK05786HTSC_JA3A
    

    或者,删除以下引号awk

    $ awk '{gsub(/"/,""); print $5}' file 
    ADMK05786HTREDIRECT_JA3A
    KOPYUTRCEPORT_JA3A
    ADMK05786HTSRM_JA3A
    ADMK05786HTIAS_JA3A
    ADMK05786HTOTN_JA3A
    ADMK05786HTWPM
    OCAIDEKD_JA3A
    ADMK05786HTSC_JA3A
    
  3. 只需打印每行上所有带引号的字符串

    $ grep -oP '"\K[^"]+(?=")' file
    ADMK05786HTREDIRECT_JA3A
    KOPYUTRCEPORT_JA3A
    ADMK05786HTSRM_JA3A
    ADMK05786HTIAS_JA3A
    ADMK05786HTOTN_JA3A
    ADMK05786HTWPM
    OCAIDEKD_JA3A
    ADMK05786HTSC_JA3A
    

答案2

grep -o '"[^"]\+"' 

这将返回双引号之间的信息。肯定有更强大的方法可以做到这一点,但如果数据始终采用该格式,它就会起作用,即使名称发生变化它也会起作用。

相关内容