LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
 
Search this Thread
Old 11-18-2012, 02:05 PM   #1
a21210
LQ Newbie
 
Registered: Nov 2012
Location: Lawrence, Kansas
Distribution: Ubuntu 12.04
Posts: 3

Rep: Reputation: Disabled
Organizing music files alphabetically but skipping the "The_" in artist names


Hey everyone!
I'm totally new to linux but I decided to give it a go full time now. I'm using Ubuntu 12.04 and I have been trying to learn to use the shell. So far everything is making sense but this one thing. Just for practice and what-not I made a .txt file with a long list format of my music directory and I was wondering how I could go about sorting all of the artists alphabetically while ignoring the "The_" in the beginning of many artist's names. You know, putting "The_Beatles" up by "Billy_Joel" instead of next to "Three_Doors_Down". I have been experimenting with wildcards associated with ls but that doesn't seem to be the answer. I have really just been toying with various incarnations of $ls -l > Music.txt Sorry if this seems like a really stupid question but any help is greatly appreciated!
 
Old 11-18-2012, 03:06 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello a21210, welcome to LQ,

if I've understood your question correctly, you want to sort the files this way, but not rename?

This should be possible with sed
Code:
ls | sed 's/^the_//i' | sort
Markus

BTW: its not a stupid question
 
3 members found this post helpful.
Old 11-18-2012, 03:14 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Hi and welcome to LinuxQuestions!

The sort command hasn't got any option to exclude a pattern from the sorting process. A workaround might be:
Code:
sed '/^The_/{s/^The_//;s/$/@/}' file | sort | sed '/@/{s/^/The_/;s/@//}'
Basically it removes the article from the artist name and marks the line with a trailing @. Then it makes sorting and adds the article back to the marked lines (and removes the trailing @). Just an idea.
 
2 members found this post helpful.
Old 11-19-2012, 10:32 AM   #4
a21210
LQ Newbie
 
Registered: Nov 2012
Location: Lawrence, Kansas
Distribution: Ubuntu 12.04
Posts: 3

Original Poster
Rep: Reputation: Disabled
YESSS! You guys rock! Ok, so I wanted to save this list as a text file for fun and I could NOT get it to work in just one line or with just one file so this is what I ended up using colucix's idea but just spread it out a bit. It worked like a champ and I learned a ton about using the sed command at the same time. Thanks again guys!

Code:
Music$ ls > music4.txt
Music$ sed '/^The /{s/^The //;s/$/@/}' <music4.txt >music3.txt
Music$ sort music3.txt > music2.txt
Music$ sed '/@/{s/^/The /;s/@//}' <music2.txt >music.txt
Music$ rm music2.txt music3.txt music4.txt
 
Old 11-19-2012, 10:52 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Try
Code:
ls | sed 's/^the_//i' | sort > mymusic.txt
but with my code the "the_" at the beginning are lost. It should be possible to do the same with colucix' code
Code:
sed '/^The_/{s/^The_//;s/$/@/}' file | sort | sed '/@/{s/^/The_/;s/@//}' > mymusic.txt
where "file" is a list with your music which can be obtained simply with
Code:
ls > file
Markus

Edit: checked with my Music directories, it works

Last edited by markush; 11-19-2012 at 10:58 AM.
 
1 members found this post helpful.
Old 11-19-2012, 11:11 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 948
Blog Entries: 2

Rep: Reputation: 226Reputation: 226Reputation: 226
In Perl or C you can define the (callback) comparison function to use in a sort and you could arrange for such a function to discard "The " at the start of a name.

See bug 5 - http://www.asktog.com/Bughouse/10Mos...esignBugs.html
 
Old 11-19-2012, 11:25 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Quote:
Originally Posted by linosaurusroot View Post
In Perl or C you can define the (callback) comparison function to use in a sort and you could arrange for such a function to discard "The " at the start of a name.

See bug 5 - http://www.asktog.com/Bughouse/10Mos...esignBugs.html
When it comes to Perl, you should take a look at the so called "Schwartzian Transform" from Randal L. Schwartz. The principle is (in short) decorate -> sort -> undecorate. This is a true Perl solution http://www.stonehenge.com/merlyn/UnixReview/col64.html and btw the same what colucix did with sed and sort.

Markus

Last edited by markush; 11-19-2012 at 11:26 AM.
 
Old 11-19-2012, 01:40 PM   #8
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 948
Blog Entries: 2

Rep: Reputation: 226Reputation: 226Reputation: 226
Quote:
Originally Posted by colucix View Post
Code:
sed '/@/{s/^/The_/;s/@//}'
2nd sed command - shouldn't /@/ be /@$/ ?
 
Old 11-19-2012, 01:46 PM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Quote:
Originally Posted by linosaurusroot View Post
2nd sed command - shouldn't /@/ be /@$/ ?
Why? there is only one @ in the line.

Markus
 
Old 11-19-2012, 01:52 PM   #10
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 948
Blog Entries: 2

Rep: Reputation: 226Reputation: 226Reputation: 226
Quote:
Originally Posted by markush View Post
Why? there is only one @ in the line.
How do you know there won't be @ in the middle of some line? "Three F@ Penguins" might have a hit song and then you're stuck.
 
Old 11-19-2012, 01:57 PM   #11
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Well, in this case one had to insert $. But the idea behind the code is to use a character which is used only for this decoration. If I knew that @ would be in the filenames, I would chose another character, for example #. Consider what would happen if @ were in the end of the filename.

Markus
 
Old 11-19-2012, 02:55 PM   #12
GazL
Senior Member
 
Registered: May 2008
Posts: 3,600

Rep: Reputation: 1093Reputation: 1093Reputation: 1093Reputation: 1093Reputation: 1093Reputation: 1093Reputation: 1093Reputation: 1093
I'd do it like this:
Code:
sed 's/\(^[Tt][Hh][Ee][-_ ]\)\(.*\)/\2\t\1\2/' input.list | sort -f | cut -f2
 
Old 11-19-2012, 04:18 PM   #13
a21210
LQ Newbie
 
Registered: Nov 2012
Location: Lawrence, Kansas
Distribution: Ubuntu 12.04
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
Code:
sed '/^The_/{s/^The_//;s/$/@/}' file | sort | sed '/@/{s/^/The_/;s/@//}' > mymusic.txt
where "file" is a list with your music which can be obtained simply with
Code:
ls > file
Markus

Edit: checked with my Music directories, it works
Yeah, the 'file' thing is what I messed up with that one-line code, this works perfect! Thanks, Markus for clearing this up for me!
 
  


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
rsync "skipping non-regular files" with --archive option andrewtblake Linux - Server 2 04-29-2010 04:15 PM
Replace ":" from multiple files names (even recursively in directories) pepeq Linux - General 3 02-01-2010 11:09 AM
"cannot access files or folders of an NTFS Win XP share if their names contain space" ramanujank Linux - Newbie 2 09-03-2009 02:09 PM
How can I put "Wave" music files on a AUDIO CD to play in my car? Balarabay1 Suse/Novell 4 05-22-2006 10:19 PM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 03:04 AM


All times are GMT -5. The time now is 02:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration