如何从包含文本的文件中提取行

问题描述 投票:0回答:1

我正在尝试查找某种类型的文件是否包含字符串并希望从文件中提取整个匹配行

要获取包含文本的文件列表,我尝试使用

find
模块。这正在发挥作用。但我还没有找到一种方法来从文件中提取所有行并将它们分配给包含该字符串的数组

- name: Verify that string out of memory exists in file /var/log/messages
  find:
  paths: /var/log/
  patterns: 
    - messages.*
    - messages
  file_type: file
  use_regex: true
  read_whole_file: true
  contains: "Out of memory"
  register: strfound

我尝试查看是否可以使用 slurp,但这些文件很大,而且 slurp 还会以 Base64 格式在屏幕上显示整个文件,因此没有继续使用该选项。

有没有办法让另一个任务从上述任务中找到的文件列表中提取行并将它们放入数组中?

ansible pattern-matching extract
1个回答
-1
投票

除了解析问题之外,slurping 会使所需的 RAM 量增加一倍,这在日志较大的情况下是不需要的。

如果您不介意使用

fgrep
,快速解决方案将如下所示(修改路径以进行测试):

    - name: Verify that string out of memory exists in file /var/log/messages
      find:
        paths: "/Users/alexander/Library/Application Support/JetBrains/IntelliJIdea2023.3/scratches/messages"
        patterns:
          - messages.*
          - messages
        file_type: file
        use_regex: true
        read_whole_file: true
        contains: "{{ string_to_search }}"
      register: strfound

    - name: Extract the lines
      when: strfound is defined and strfound.files
      block:
        - name: Grep the files
          command: "fgrep '{{ string_to_search }}' '{{ item.path }}'"
          register: search_results
          loop: "{{ strfound.files }}"
          loop_control:
            label: "{{ item.path }}"

        - name: Collect the found lines
          set_fact:
            found_lines: "{{ found_lines | default([]) + found_file.stdout_lines }}"
          loop: "{{ search_results.results }}"
          loop_control:
            loop_var: "found_file"
            label: "{{ found_file.item.path }}"

        - name: Log the final result
          debug:
            var: found_lines

结果如下:

TASK [Collect the found lines] ***************************************************************************************************************************************************************************************************
ok: [localhost] => (item=/Users/alexander/Library/Application Support/JetBrains/IntelliJIdea2023.3/scratches/messages/messages.log)
ok: [localhost] => (item=/Users/alexander/Library/Application Support/JetBrains/IntelliJIdea2023.3/scratches/messages/messages.with.token.log)

TASK [Log the final result] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "found_lines": [
        "  contains: \"Out of memory\"",
        "another \"Out of memory\" entry",
        "and one more \"Out of memory\" entry"
    ]
}

© www.soinside.com 2019 - 2024. All rights reserved.