I have a file/output containing this :
igw_id = igw-96788cf1 private_route_tables_ids = [ rtb-c2adcda4, rtb-c5a3c3a3, rtb-c4adcda2 ] private_subnets_cidrs_ipv4 = [ 10.20.10.0/24, 10.20.11.0/24, 10.20.12.0/24 ] private_subnets_ids = [ subnet-6057333b, subnet-6be7bf0c, subnet-f13419b8 ] public_route_tables_ids = [ rtb-74a9c912, rtb-c5adcda3, rtb-2aabcb4c ] public_subnets_cidrs_ipv4 = [ 10.20.0.0/24, 10.20.1.0/24, 10.20.2.0/24 ] public_subnets_ids = [ subnet-6157333a, subnet-17e7bf70, subnet-303f1279 ]
I would like to extract all public subnet id and print them without, and white space.
I used this regex
sed -n '/public_subnets_ids/{:a;N;/]/!ba;s/[[:space:]]//g;s/,/n/g;s/.*public_subnets_ids|].*//g;p}' my_file.txt
And the output is :
=[subnet-6157333a subnet-17e7bf70 subnet-303f1279
But I would like to get this instead:
subnet-6157333a subnet-17e7bf70 subnet-303f1279
In fact I told sed to replace spaces and newlines with nothing (s/[[:space:]]//g)
and then it also replace the first new line and then brings the first subnet up, so I would to process the regex after the first newline and when I try this
sed -n '/public_subnets_ids = [[nrs]+\n/{:a;N;/]/!ba;s/[[:space:]]//g;s/,/n/g;s/.*public_subnets_ids|].*//g;p}' my_file.txt
It gives no ouput, means it doesn’t match anything.
Please can help improve the above regex to give only subnets ids in seperated lines?
Advertisement
Answer
Another approach this sed:
sed -n '/public_subnets_ids/,/]/{//!{s/[ ,]*//g;p;}}' file
'/public_subnets_ids/,/]/
: from line containingpublic_subnets_ids
up to next line containing]
//!
: in matching lines, except those matching the addresses//!{s/[ ,]*//g;p;}
: removes commas and space characters and output result