LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-20-2009, 10:20 AM   #1
oraerk
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:
Code:
./.VirtualBox/Machines/eData/Logs/VBox.log
./.gftp/gftp.log
./.kde/share/apps/kconf_update/log/update.log
I need to remove patterns in bold, so the result should be:
Code:
./.VirtualBox/Machines/eData/Logs/
./.gftp/
./.kde/share/apps/kconf_update/log/
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:
Code:
.
.
.
What am I doing wrong?

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

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

in sed you can have
Code:
sed 's/[^\/]\+$//' in1
sed 's@[^/]@\+$@' in1
in bash perhaps you can use
Code:
while read LINE; do
    echo "${LINE%[^/]}"
done
not sure though. just test.
 
Old 09-20-2009, 11:48 AM   #3
oraerk
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:
Code:
+$/.VirtualBox/Machines/eData/Logs/VBox.log
+$/.gftp/gftp.log
+$/.kde/share/apps/kconf_update/log/update.log
In any case, dirname is what i really needed, thanks for the help!
 
  


Reply

Tags
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


All times are GMT -5. The time now is 03:18 PM.

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