Reputation: 99
If I have string:
path1=/path/me & you/file.json&path2=/path/you & me/file.txt
I expect the output to be like this:
path1=/path/me & you/file.json;path2=/path/you & me/file.txt
I need to replace &
that it's front and back not contain space
, I tried with sed
but I keep got this
path1=/path/me ; you/file.json;path2=/path/you ; me/file.txt
Upvotes: 0
Views: 115
Reputation: 446
sed -r 's/(\S)&(\S)/\1;\2/g'
Where -r enable regex and \S is all except spaces.
Upvotes: 0
Reputation: 189377
I'm guessing maybe you are looking for
sed 's/&\([a-z][a-z0-9]*=\)/;\1/g'
i.e. replace only semicolons which are immediately followed by a token and an equals sign. (You may have to adjust the token definition, depending on what your tokens can look like. For example, underscore is often supported, but rarely used. You mght want to support uppercase, too.)
If at all possible, fix the process which produces these values, as the format is inherently ambiguous.
Upvotes: 0
Reputation: 123460
You can use [^ ]
to match a non-space character and make sure it's in a \(capture group\)
so that you can reference it in the replacement string:
sed -e 's/\([^ ]\)&\([^ ]\)/\1;\2/'
This finds any three character sequence of non-space
&
non-space
and replaces it just the two captured characters, effectively replacing any &
without a space next to it.
This will affect foo&bar
but not foo & bar
or foo& bar
or &foo
Upvotes: 1