How do I extract multiple pieces of information using a regex?
I need to extract multiple pieces of information from a returned collection of strings. The strings consist of a number of “fields”, for example:
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.10:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-xvz7v", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.11:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-w8cqq", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.12:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-4wfhq", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.13:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-zfskj", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.15:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-cllnv", service="prometheus-prometheus-node-exporter"}
In this case, I want to extract the label and value of the fstype
, instance
and mountpoint
fields. So for the example provided, I’d end up with something like the following:
fstype="ext4" instance="172.20.32.10:9100" mountpoint="/"
fstype="ext4" instance="172.20.32.11:9100" mountpoint="/"
fstype="ext4" instance="172.20.32.12:9100" mountpoint="/"
fstype="ext4" instance="172.20.32.13:9100" mountpoint="/"
fstype="ext4" instance="172.20.32.15:9100" mountpoint="/"
Can this be done with a regular expression? I’ve used REs for a long time, but never anything this complex. If the answer to that question is yes, how do I do it?
Read more here: Source link