LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed or awk delete after last slash (https://www.linuxquestions.org/questions/linux-newbie-8/sed-or-awk-delete-after-last-slash-749560/)

kofucii 08-22-2009 12:25 PM

sed or awk delete after last slash
 
Hello,
How can I delete everything after the last slash of a line. For example
form:
/tmp/dir/test
to:
/tmp/dir

Or from:
/dir/tmp/dir/test
To:
/dir/tmp/dir

I know how to delete everything before the last slash:
sed 's!.*/!!'

But how to do it after last the slash?

acid_kewpie 08-22-2009 12:27 PM

use the "dirname" command instead, purpose made for exactly that.

pixellany 08-22-2009 03:49 PM

sed 's%/[^/]*$%/%'

(Find "/", followed by any number of characters that are NOT "/", at the end of the line; Replace with "/")


All times are GMT -5. The time now is 10:55 AM.