Operating on a subset of lines with sed

Today a coworker posed an interesting question to me. Given a BIG-IP configuration file with thousands of records like this… gtm pool /Common/p_example { members { /Common/2001:668:fc78:0:f811:3ef2:fed7:98cc:vs6_2001_668_fc78_0_f811-3ef2-fed7_98cc-80 { order 0 } } monitor /Common/gateway_icmp } …how can we easily replace every colon in the name : with \\:? In short, /Common/2001:668:fc78:0:f811:3ef2:fed7:98cc:vs6_2001_668_fc78_0_f811-3ef2-fed7_98cc-80 needed to become /Common/2001\\:668\\:fc78\\:0\\:f811\\:3ef2\\:fed7\\:98cc\\:vs6_2001_668_fc78_0_f811-3ef2-fed7_98cc-80. This must only be done on the lines with start with /Common and which also contain the string vs6....

2019-09-11

Notes on sed

I’ve always been a bit hit-or-miss with sed and haven’t understood it to the level to which I wanted. Today I aimed to change that and these are my notes from sitting down to learn it. Replace a string in a file (first occurrence per line only): cat file.txt | sed 's/oldstring/newstring/' Replace a string in a file (all occurrences per line, i.e., globally): cat file.txt | sed 's/oldstring/newstring/g' Use your own delimiters by just dropping them in place of /:...

2019-06-15