LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to delete filename from string with absolute path using sed? (https://www.linuxquestions.org/questions/programming-9/how-to-delete-filename-from-string-with-absolute-path-using-sed-756563/)

oraerk 09-20-2009 09:20 AM

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?

konsolebox 09-20-2009 09:39 AM

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.

oraerk 09-20-2009 10:48 AM

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!


All times are GMT -5. The time now is 12:52 AM.