LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Removing beginning of line with a shell script (http://www.linuxquestions.org/questions/programming-9/removing-beginning-of-line-with-a-shell-script-551621/)

oscarlevin 05-05-2007 09:33 PM

Removing beginning of line with a shell script
 
Hello,

I am trying to write a script to do the following: given a m3u playlist file, strip off the path of each song listed and leave only the filename itself. So what I want to do, is delete the beginning of each line up through the last occurrence of "/".

Thing is, I already had a script that did just this (but alas, I lost it). I remember when I wrote the script, I was really impressed with how easy it was to do - even bragged to my friends about how awesome Linux was for allowing me to write such a simple script. But now I can't find anything like what I found last time.

Any ideas?

Thanks,
Oscar.

jlinkels 05-05-2007 09:41 PM

basename?

jlinkels

jaykup 05-06-2007 02:29 AM

basename it is!

One Liner:

Code:

cat playlist.m3u | grep -v EXTINF | sed "s|'|'\\\''|;s|^|basename '|;s|$|'|" | bash
or a readable script

Code:

#!/bin/bash

# If no file is specified, print this and exit
if [ $# -eq 0 ]
        then
        echo "Usage: basename $0 [playlist.m3u]"
        exit 1
fi

# Formats the file, puts basename in front of each line, and
# Runs it with bash.
cat $1 |
grep -v EXTINF |
sed "s|'|'\\\''|;s|^|basename '|;s|$|'|" |
bash

Call with ./script_name playlist.m3u

Tinkster 05-06-2007 03:15 AM

Code:

awk -F/ '!/^#/ {print $NF}' m3u


Cheers,
Tink

jaykup 05-06-2007 03:20 AM

Quote:

Originally Posted by Tinkster
Code:

awk -F/ '!/^#/ {print $NF}' m3u


Cheers,
Tink

.. that is so cool. I really need to read up on awk

oscarlevin 05-06-2007 03:55 AM

Thanks guys. I'll give those a shot as soon as I get back to my computer. This forum is the BEST!

Oscar.

Tinkster 05-06-2007 03:56 AM

Quote:

. that is so cool. I really need to read up on awk
There's (always) more than one way to do it ;}

Glad you liked my approach.



Cheers,
Tink

radoulov 05-06-2007 04:36 AM

Quote:

Originally Posted by oscarlevin
[...]
given a m3u playlist file, strip off the path of each song listed and leave only the filename itself. So what I want to do, is delete the beginning of each line up through the last occurrence of "/".
[...]

With bash and *no* pathological filenames you could:

Code:

$ cat list
/dir/dir1/regular_name.mp3
/dir2/regular_name_1.mp3
./regular_name_2.mp3

$ set -- $(<list)

$ printf "%s\n" "${@##*/}"
regular_name.mp3
regular_name_1.mp3
regular_name_2.mp3


If you have weird filenames:

Code:

$ cat list
/dir/dir1/regular_name.mp3
/dir2/filename with space.mp3
/dir/dir2/filename with embedded new
        line and spaces.mp3

$ while IFS= read -r;do printf "%s\n" "${REPLY##*/}";done<list
regular_name.mp3
filename with space.mp3
filename with embedded new
        line and spaces.mp3


jaykup 05-06-2007 04:44 PM

This is interesting:

Code:

root@digital:~# cat playlist.m3u | wc -l
7699
root@digital:~# time cat playlist.m3u | grep -v EXTINF | sed "s|'|'\\\''|g;s|^|basename '|;s|$|'|" | bash > /dev/null

real    0m2.699s
user    0m1.021s
sys    0m1.724s
root@digital:~# time awk -F/ '!/^#/ {print $NF}' playlist.m3u > /dev/null

real    0m0.009s
user    0m0.008s
sys    0m0.001s
root@digital:~#

If I don't do a global replace with sed, the time cuts down to .333 seconds, but then I get some errors if the file has more than one ' in it.

Pretty amazing!

Tinkster 05-07-2007 01:18 AM

Which kind of makes the use of awk for certain tasks look
pretty darn good ;}


Cheers,
Tink


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