Bindu G
Bindu G

Reputation: 103

Regex to extract lines between two patterns

I have playbook output (exp.stdout_lines) as

    "stdout_lines": [
        "exit",
        "logout",
        "",
        "df START",
        "df -h",
        "Filesystem      Size  Used Avail Use% Mounted on",
        "udev            476M     0  476M   0% /dev",
        "tmpfs           100M   11M   89M  11% /run",
        "/dev/sda1       218G  1.9G  205G   1% /",
        "tmpfs           497M     0  497M   0% /dev/shm",
        "tmpfs           5.0M     0  5.0M   0% /run/lock",
        "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
        "/dev/sda2       923M   59M  802M   7% /boot",
        "/dev/sda4       266G  306M  252G   1% /home",
        "tmpfs           100M     0  100M   0% /run/user/1001",
        "\u001b]0;",
        "",
        "df END",
        "running ls commnd",
        "ls",
        "bhr",
        "\u001b]0;",
        "",
        "completed ls commnd"
    ]
}

I would like to grep lines between df START and df END

 - set_fact: 
        result: "{{ exp.stdout_lines | regex_search((?<=df START).*(?=df END)) }}"

I am seeing below error"

"template error while templating string: unexpected char '?' at 36. String: {{ exp.stdout_lines | regex_search((?<=df START).*(?=df END)) }}"}

Any suggestions.

Upvotes: 1

Views: 916

Answers (1)

Rickkwa
Rickkwa

Reputation: 2291

Instead of using regular expressions, I'm thinking we can use list slicing.

So from exp.stdout_lines list, we find the index of df START with exp.stdout_lines.index('df START'). And similarly for df END. Then we can use exp.stdout_lines[startIndex + 1 : endIndex] to get the elements strictly between df START and df END.

- hosts: localhost
  vars:
    exp:
      stdout_lines: [
          "exit",
          "logout",
          "",
          "df START",
          "df -h",
          "Filesystem      Size  Used Avail Use% Mounted on",
          "udev            476M     0  476M   0% /dev",
          "tmpfs           100M   11M   89M  11% /run",
          "/dev/sda1       218G  1.9G  205G   1% /",
          "tmpfs           497M     0  497M   0% /dev/shm",
          "tmpfs           5.0M     0  5.0M   0% /run/lock",
          "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
          "/dev/sda2       923M   59M  802M   7% /boot",
          "/dev/sda4       266G  306M  252G   1% /home",
          "tmpfs           100M     0  100M   0% /run/user/1001",
          "\u001b]0;",
          "",
          "df END",
          "running ls commnd",
          "ls",
          "bhr",
          "\u001b]0;",
          "",
          "completed ls commnd"
      ]
  tasks:
    - debug:
        msg: "{{ exp.stdout_lines[exp.stdout_lines.index('df START') + 1 : exp.stdout_lines.index('df END')] }}"
ok: [localhost] => {
    "msg": [
        "df -h",
        "Filesystem      Size  Used Avail Use% Mounted on",
        "udev            476M     0  476M   0% /dev",
        "tmpfs           100M   11M   89M  11% /run",
        "/dev/sda1       218G  1.9G  205G   1% /",
        "tmpfs           497M     0  497M   0% /dev/shm",
        "tmpfs           5.0M     0  5.0M   0% /run/lock",
        "tmpfs           497M     0  497M   0% /sys/fs/cgroup",
        "/dev/sda2       923M   59M  802M   7% /boot",
        "/dev/sda4       266G  306M  252G   1% /home",
        "tmpfs           100M     0  100M   0% /run/user/1001",
        "\u001b]0;",
        ""
    ]
}

If you want the result to be a string instead of a list, you can pipe it to the join('\n') filter.

Upvotes: 1

Related Questions