Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-20-2009, 10:20 AM   #1
LQ Newbie
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
how to delete filename from string with absolute path using sed?

Hello, everyone!

I have the following problem. I've a file containing lots of similar string, actually they are a paths to files. I need to remove the filename from it. Just can't figure out how to do that.

So, for clearance, there is an example input file in1:
I need to remove patterns in bold, so the result should be:
I figured out how to get only filenames, that is easily reached by the following command: sed s/^.*\\/// in1

I've tried to do so to guess inverse sed s/\\/.*$// in1, but that leads only to the following output:
What am I doing wrong?

Last edited by oraerk; 09-20-2009 at 10:23 AM.
Old 09-20-2009, 10:39 AM   #2
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
have you tried using dirname?

in sed you can have
sed 's/[^\/]\+$//' in1
sed 's@[^/]@\+$@' in1
in bash perhaps you can use
while read LINE; do
    echo "${LINE%[^/]}"
not sure though. just test.
Old 09-20-2009, 11:48 AM   #3
LQ Newbie
Registered: Sep 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a lot! How could i forgotten about dirname?! ) That did exactly what I needed.
But never the less, I've tested sed 's/[^\/]\+$//' in1, that worked just the same. Though, I have not succeeded with sed 's@[^/]@\+$@', the output is:
In any case, dirname is what i really needed, thanks for the help!


filename, path, regex, regexp, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
java applet not loading image with relative path but loads image with absolute path amolgupta Programming 2 07-20-2009 02:58 PM
find string in filename and use string to create directories daberkow Linux - Newbie 11 05-01-2009 03:12 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-13-2008 12:32 AM
how to use sed to delete mutiple string patterns from apache access log matyu Programming 3 01-06-2008 12:42 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 08:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration