Is there a way to substitute only within the match space using sed?
I.e. given the following line, is there a way to substitute only the “.” chars that are contained within the matching single quotes and protect the “.” chars that are not enclosed by single quotes?
Input:
'ECJ-4YF1H10.6Z' ! 'CAP' ! '10.0uF' ! 'TOL' ; MGCDC1008.S1 MGCDC1009.A2
Desired result:
'ECJ-4YF1H10-6Z' ! 'CAP' ! '10_0uF' ! 'TOL' ; MGCDC1008.S1 MGCDC1009.A2
Or is this just a job to which perl or awk might be better suited?
Thanks for your help,
Mark
Give the following a try which uses the divide-and-conquer technique:
Explanation:
s/\('[^']*'\)/\n&\n/g– Add newlines before and after each pair of single quotes with their contentss/\(\n'[^.]*\)\.\([^']*Z'\)/\1-\2/g– Using a newline and the single quotes to key on, replace the dot with a dash for strings that end in “Z”s/\(\n'[^.]*\)\.\([^']*uF'\)/\1_\2/g– Using a newline and the single quotes to key on, replace the dot with a dash for strings that end in “uF”s/\n//g– Remove the newlines added in the first stepYou can restrict the command to acting only on certain lines:
where you would substitute some regex in place of “foo”.
Some versions of
sedlike to be spoon fed (instead of semicolons between commands, use-e):