Home > other >  How to remove excess garbage from a variable?
How to remove excess garbage from a variable?

Time:12-09

How can I remove the extra quotes and commas so that only useful information remains? Here is a variable to work with:

"ping.stdout_lines": [
    [
        "p",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "  SEQ HOST                                 SIZE TTL TIME  STATUS           ",
        "    0 192.168.0.1                            56  64 1ms  ",
        "    1 192.168.0.1                            56  64 1ms  ",
        "    2 192.168.0.1                            56  64 1ms  ",
        "    3 192.168.0.1                            56  64 1ms  ",
        "    4 192.168.0.1                            56  64 1ms  ",
        "    sent=5 received=5 packet-loss=0% min-rtt=1ms avg-rtt=1ms max-rtt=1ms"
    ]
]

I tried to do it through regex, but nothing comes out.

Desired result:

        SEQ HOST                                   SIZE TTL TIME  STATUS           
            0 192.168.0.1                            56  64 1ms  
            1 192.168.0.1                            56  64 1ms  
            2 192.168.0.1                            56  64 1ms  
            3 192.168.0.1                            56  64 1ms  
            4 192.168.0.1                            56  64 1ms  
            sent=5 received=5 packet-loss=0% min-rtt=1ms avg-rtt=1ms max-rtt=1ms

I can't figure it out in any way.

CodePudding user response:

Simply use select without a test it rejects empty lines, e.g.

    - debug:
        msg: "{{ item|select }}"
      loop: "{{ ping.stdout_lines }}"

gives

  msg:
  - p
  - '  SEQ HOST                                 SIZE TTL TIME  STATUS           '
  - '    0 192.168.0.1                            56  64 1ms  '
  - '    1 192.168.0.1                            56  64 1ms  '
  - '    2 192.168.0.1                            56  64 1ms  '
  - '    3 192.168.0.1                            56  64 1ms  '
  - '    4 192.168.0.1                            56  64 1ms  '
  - '    sent=5 received=5 packet-loss=0% min-rtt=1ms avg-rtt=1ms max-rtt=1ms'

If you want to keep the filtered data in a variable

  pings: "{{ ping.stdout_lines|map('select')|list }}"

gives

  pings:
  - - p
    - '  SEQ HOST                                 SIZE TTL TIME  STATUS           '
    - '    0 192.168.0.1                            56  64 1ms  '
    - '    1 192.168.0.1                            56  64 1ms  '
    - '    2 192.168.0.1                            56  64 1ms  '
    - '    3 192.168.0.1                            56  64 1ms  '
    - '    4 192.168.0.1                            56  64 1ms  '
    - '    sent=5 received=5 packet-loss=0% min-rtt=1ms avg-rtt=1ms max-rtt=1ms'
  • Related