代码之家  ›  专栏  ›  技术社区  ›  user3847894

在=[重复]之后打印所有内容

  •  -2
  • user3847894  · 技术社区  · 6 年前

    我正在尝试解析一个文本文件,其中需要获取“inventory=”之后的所有内容。这个问题可能看起来很简单,但我不擅长解析文本文件,因此我提出了这个问题。

    输入文件

    [root@localhost ~]# cat file
    inventory = #########################################
    
    
            Ansible Inventory File
    
    
    #########################################
    [targets]
    localhost      ansible_connection=local
    192.168.44.134
    192.168.44.200
    
    [jewels]
    192.168.44.200  ansible_connection=local        abc=test
    192.168.44.134
    
    [apple]
    localhost       ansible_connection=local
    0               ansible_connection=local
    
    [fruits:children]
    jewels
    apple
    

    [root@localhost ~]# cat file
    
     #########################################
    
    
             Ansible Inventory File
    
    
     ######################################### 
     [targets] 
     localhost      ansible_connection=local
     192.168.44.134
     192.168.44.200
    
     [jewels]
     192.168.44.200  ansible_connection=local        abc=test
     192.168.44.134
    
     [apple] localhost       ansible_connection=local 0              
     ansible_connection=local
    
     [fruits:children] jewels apple
    
    4 回复  |  直到 6 年前
        1
  •  1
  •   Manish R    6 年前

    试试这个

    awk 'NR==1{split($0,arr," = "); print arr[2]; while (getline == 1) print $0}' file
    

    getline将从第二行开始读取,直到该行结束

    awk 'NR==1{for(i=1; i<=42; i++) printf "#"; while (getline == 1) print $0}' file
    
        2
  •  1
  •   Ed Morton    6 年前

    根据你的描述 I need to get all the content after "inventory = " 听起来这正是你想要的,因为它会做到:

    awk 'sub(/.*inventory = /,""){f=1} f' file
    

        3
  •  1
  •   stack0114106    6 年前

    尝试Perl,因为它是这些情况的正确选择。

    perl -0777 -ne ' /inventory =(.*)/s and print $1 ' 
    

    $ cat inventory.txt
    inventory = #########################################
    
    
            Ansible Inventory File
    
    
    #########################################
    [targets]
    localhost      ansible_connection=local
    192.168.44.134
    192.168.44.200
    
    [jewels]
    192.168.44.200  ansible_connection=local        abc=test
    192.168.44.134
    
    [apple]
    localhost       ansible_connection=local
    0               ansible_connection=local
    
    [fruits:children]
    jewels
    apple
    $  perl -0777 -ne ' /inventory =(.*)/s and print $1 ' inventory.txt
     #########################################
    
    
            Ansible Inventory File
    
    
    #########################################
    [targets]
    localhost      ansible_connection=local
    192.168.44.134
    192.168.44.200
    
    [jewels]
    192.168.44.200  ansible_connection=local        abc=test
    192.168.44.134
    
    [apple]
    localhost       ansible_connection=local
    0               ansible_connection=local
    
    [fruits:children]
    jewels
    apple
    $
    
        4
  •  0
  •   RavinderSingh13 Nikita Bakshi    6 年前

    你能试试下面的吗。我假设你只是在找绳子 invetory=

    awk '/^inventory =/{sub(/.*inventory = +/,"");found=1} found'  Input_file