I’m trying to use sed to replace whitespace within a string. For example, given the line:
var test = 'Some test text here.';
I want to get:
var test = 'Sometesttexthere.';
I’ve tried using (\x27 matches the '):
sed 's|\x27\([^\x27[:space:]]*\)[[:space:]]|\x27\1|g
but that just gives
var test = 'Sometest text here.';
Any ideas?
This is a much more complex
sedscript, but it works without a loop. You know, just for the sake of variety:It makes a copy of the string, splits one (which will become the second half) at the first single quote discarding the first half, replaces all the spaces in the second half, swaps the copies, splits the other one discarding the second half, merges them back together and removes the newlines used for the splitting and the one added by the
Gcommand.Edit:
In order to select particular lines to operate on, you can use some selection criteria. Here I’ve specified that the line must contain an equal sign and at least two single quotes:
You could use whatever regex works best to include and exclude appropriately for your needs.