How to remove last directory from a path with sed?
How to remove the last dir with sed
(not dirname
), like this:
echo "/d开发者_StackOverflowir1/dir2/dir3/dir4" | sed .....
So I would get /dir1/dir2/dir3
.
you don't have to use external tools
$ a="/dir1/dir2/dir3/dir4"
$ echo ${a%/*}
sed 's,/*[^/]\+/*$,,'
If it's part of the shell script, then dirname
will be definitely more clear.
you can use "dirname" shell command:
dirname /dir2/dir3/dir4
echo "/etc1/etc2/etc3/etc" | sed -e "s/\/[^\/]*$//"
produces
/etc1/etc2/etc3
Basically strip off anything at the end after the last slash that doesn't contain another slash.
精彩评论