|
Home > Archive > Unix Shell > December 2007 > sed: Removing trailing "/"
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sed: Removing trailing "/"
|
|
|
| Can somebody provide me with a sed command that will remvove a
trailing slash.
For example if I have this /tmp/one/two/three/ I want to make it /tmp/
one/two/three
Thanks in advance for all that answer this post
| |
| Cyrus Kriticos 2007-12-06, 7:25 pm |
| Stu wrote:
> Can somebody provide me with a sed command that will remvove a
> trailing slash.
>
> For example if I have this /tmp/one/two/three/ I want to make it /tmp/
> one/two/three
$ echo "/tmp/one/two/three/" | sed "s,/$,,"
/tmp/one/two/three
--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
| |
| Glenn Jackman 2007-12-06, 7:25 pm |
| At 2007-12-06 04:17PM, "Stu" wrote:
> Can somebody provide me with a sed command that will remvove a
> trailing slash.
>
> For example if I have this /tmp/one/two/three/ I want to make it /tmp/
> one/two/three
If you really want sed:
x="/tmp/one/two/three/"
echo "$x" | sed -e 's,/$,,'
However, it would be more efficient to let your shell do it -- in bash
(and I'm sure many other shells): echo "${x%/}"
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
| Stephane Chazelas 2007-12-07, 7:31 am |
| On 6 Dec 2007 21:27:39 GMT, Glenn Jackman wrote:
> At 2007-12-06 04:17PM, "Stu" wrote:
>
> If you really want sed:
> x="/tmp/one/two/three/"
> echo "$x" | sed -e 's,/$,,'
>
> However, it would be more efficient to let your shell do it -- in bash
> (and I'm sure many other shells): echo "${x%/}"
That's a POSIX feature. So, you'll find it in any standard sh.
Only the Bourne shell and early versions of the Almquist shells
(BSD shells) do not support it.
A problem with those approaches is that it doesn't work
correctly if the input is "/". (and the usage of "echo" makes it
unreliable). The sed approach doesn't work correctly with
filenames containing newline characters.
One can also use expr:
expr "x$x" : 'x\(.*\)/'
which suffers from the same problem wrt x=/
case $x in
(*[!/]*/)
y=${x##*[!/]}
y=${x%"$y"} ;;
("") y= ;;
(*) y=/ ;;
esac
would remove every trailing /.
You can also do:
printf '%s\n' "$x" | sed '
s,//*,/,g
$!b
1{
/^\/$/q
}
s,/$,,'
Which will additionaly reduce every sequence of / to a single
one.
--
Stephane
| |
| Michael Tosch 2007-12-08, 7:42 pm |
| Stephane Chazelas wrote:
> On 6 Dec 2007 21:27:39 GMT, Glenn Jackman wrote:
>
> That's a POSIX feature. So, you'll find it in any standard sh.
> Only the Bourne shell and early versions of the Almquist shells
> (BSD shells) do not support it.
>
> A problem with those approaches is that it doesn't work
> correctly if the input is "/". (and the usage of "echo" makes it
> unreliable). The sed approach doesn't work correctly with
> filenames containing newline characters.
>
> One can also use expr:
>
> expr "x$x" : 'x\(.*\)/'
>
> which suffers from the same problem wrt x=/
and from the problem
x=does/not/end/with/slash
while sed can be tweaked
echo "$x" | sed -e 's,\(.\)/$,\1,'
--
Michael Tosch @ hp : com
|
|
|
|
|