Nikhil Nikhil - 11 months ago 126
Linux Question

Linux Substitution: Add spaces between brackets in a file using sed

I would like to add spaces between my braces when there is content in the brackets. For example, I would like to substitute the following line

sorted ([] if interfaces is None else list(interfaces[temp])))


to

sorted ( [] if interfaces is None else list( interfaces[ temp ] ) ) )


I am trying to use sed inside vim but I am unable to get the regex correctly. Anyone have any idea of the pattern required to solve this?

Answer Source

In vim it would be the following two commands:

:%s/(\([ )]\)\@!/( /g
:%s/\([ (]\)\@<!)/ )/g
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download