I\'m trying to replace /./
or /././
or /./././
to /
only in bash script. I\'ve managed to create regex for sed but it doe
Use -r
option to make sed
to use extended regular expression:
$ variable="something/./././"
$ echo $variable | sed -r "s/\/(\.\/)+/\//g"
something/
Two things to make it simple:
$ variable="something/./././"
$ sed -r 's#(\./){1,}##' <<< "$variable"
something/
{1,}
to indicate one or more patterns. You won't need g
with this.#
in above case to make it readable+
is ERE so you need to enable -E
or -r
option to use itYou can also do this with bash's built-in parameter substitution. This doesn't require sed
, which doesn't accept -r
on a Mac under OS X:
variable="something/./././"
a=${variable/\/*/}/ # Remove slash and everything after it, then re-apply slash afterwards
echo $a
something/
See here for explanation and other examples.
Any sed:
sed 's|/\(\./\)\{1,\}|/|g'
But a +
or \{1,\}
would not even be required in this case, a *
would do nicely, so
sed 's|/\(\./\)*|/|g'
should suffice