开发者

SED whitespace removal within a string

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.开发者_如何学Go';

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 sed script, but it works without a loop. You know, just for the sake of variety:

sed 'h;s/[^\x27]*\x27\(.*\)/\n\x27\1/;s/ //g;x;s/\([^\x27]*\).*/\1/;G;s/\n//g'

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 G command.

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:

sed '/.*=.*\x27.*\x27.*/ {h;s/[^\x27]*\x27\(.*\)/\n\x27\1/;s/ //g;x;s/\([^\x27]*\).*/\1/;G;s/\n//g}'

You could use whatever regex works best to include and exclude appropriately for your needs.


Your command line has two problems:

  • First, there's a missing \ after [^.

  • Second, even though you use the g modifier, only the first space is removed. Why? Because that modifier leads to replacement of successive matches within the same line. It does not re-scan the whole line from the beginning. But this is required here, because your match is anchored at the initial ' of the string literal.

The obvious way to solve this problem is to use a loop, implemented by a conditional jump (jump with tLabel to a :Label; t jumps if at least one s matched since the last test with t).

This is easiest with a sed script (and you don't have to escape the '), like so:

:a
s|'\([^'[:space:]]*\)[[:space:]]|'\1|
ta

But it can be done one the command prompt. The exact syntax may depend on your sed flavour, for mine (super-sed on Windows) it is invoked like so:

sed -e ":a" -e "s|\x27\([^\x27[:space:]]*\)[[:space:]]|\x27\1|;ta"

You need two separate script expressions, because the label :a extends until the end of an expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜