Given the following text (commands in a script):
test --my-option ' my/string my-second/string '
test --my-option ' my/string my-second/string '
test --my-option 'my/string my-second/string '
test --my-option ' my/string my-second/string '
test --my-option ' my/string '
I use the following regex: --my-option ' *([^']*?) *'
with --my-option '$1'
as replacement expression to get:
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string'
Question is: how would I do this with sed?
Given the following text (commands in a script):
test --my-option ' my/string my-second/string '
test --my-option ' my/string my-second/string '
test --my-option 'my/string my-second/string '
test --my-option ' my/string my-second/string '
test --my-option ' my/string '
I use the following regex: --my-option ' *([^']*?) *'
with --my-option '$1'
as replacement expression to get:
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string'
Question is: how would I do this with sed?
This might work for you (GNU sed):
sed -E 's/^([^'\'']*'\'') *([^'\'' ]+( +[^'\'' ]+)*) *('\''.*)/\1\2\4/' file
Remove spaces to the right of the first '
and to the left of the next '
.
Alternative:
sed -E 's/^([^'\'']*'\'') *([^'\'']*\>) */\1\2/' file
Using any sed:
$ sed "s/ *' */'/g; s/'/ '/" file
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string'
Alternatively:
$ sed "s/' *\(.*[^ ]\)* *'/'\1'/" file
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string my-second/string'
test --my-option 'my/string'
sed -E "s/' *([^']*[^' ])? *'/'\1'/g"
' *
matches open singlequote and 0+ spaces([^']*[^' ])?
match group 1: 0 or 1 sequence of non-quote chars ending in a non-space char. Basically requires that match group 1 end in a char other than space, and cannot have quotes. *'
matches 0+ spaces and close singlequoteThis should handle several single-quoted strings in each line using s///g
because it has exactly two singlequote chars in the match regex.
sed -E "s/--my-option ' *([^[:space:]']*[^']*[^'[:space:]]) *'/--my-option '\1'/" file
See the matches here regex101.com/r/SnCgbU/1 – The fourth bird Commented Jan 16 at 13:14perl -pe "s/--my-option ' *([^']*?) *'/--my-option '\$1'/"
(note the backslash to prevent expansion of$1
by the shell). – Renaud Pacalet Commented Jan 16 at 13:29*?
), that sed does not support and that is helpful in cases like yours. – Renaud Pacalet Commented Jan 16 at 13:43sed "s/' */'/; s/ *'$/'/" file
? – Cyrus Commented Jan 16 at 17:28