LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-05-2007, 10:33 PM   #1
oscarlevin
LQ Newbie
 
Registered: Mar 2004
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-05-2007, 10:41 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
basename?

jlinkels
 
Old 05-06-2007, 03:29 AM   #3
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
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

Last edited by jaykup; 05-06-2007 at 03:36 AM.
 
Old 05-06-2007, 04:15 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
awk -F/ '!/^#/ {print $NF}' m3u


Cheers,
Tink
 
Old 05-06-2007, 04:20 AM   #5
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
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
 
Old 05-06-2007, 04:55 AM   #6
oscarlevin
LQ Newbie
 
Registered: Mar 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys. I'll give those a shot as soon as I get back to my computer. This forum is the BEST!

Oscar.
 
Old 05-06-2007, 04:56 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 05-06-2007, 05:36 AM   #8
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
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

Last edited by radoulov; 05-06-2007 at 05:47 AM.
 
Old 05-06-2007, 05:44 PM   #9
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
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!

Last edited by jaykup; 05-06-2007 at 05:50 PM.
 
Old 05-07-2007, 02:18 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

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


Cheers,
Tink
 
  


Reply


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 05:58 AM
shell script question: removing a line according to a duplicate "field" mattie_linux Linux - Newbie 1 01-12-2006 04:57 PM
shell script that read each line separatly xpucto Programming 6 09-20-2005 09:06 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 10:16 AM


All times are GMT -5. The time now is 05:24 PM.

Main Menu
Advertisement
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