LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Organizing music files alphabetically but skipping the "The_" in artist names (https://www.linuxquestions.org/questions/linux-newbie-8/organizing-music-files-alphabetically-but-skipping-the-the_-in-artist-names-4175437702/)

a21210 11-18-2012 02:05 PM

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!

markush 11-18-2012 03:06 PM

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

colucix 11-18-2012 03:14 PM

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.

a21210 11-19-2012 10:32 AM

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


markush 11-19-2012 10:52 AM

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

linosaurusroot 11-19-2012 11:11 AM

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

markush 11-19-2012 11:25 AM

Quote:

Originally Posted by linosaurusroot (Post 4832680)
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

linosaurusroot 11-19-2012 01:40 PM

Quote:

Originally Posted by colucix (Post 4832181)
Code:

sed '/@/{s/^/The_/;s/@//}'

2nd sed command - shouldn't /@/ be /@$/ ?

markush 11-19-2012 01:46 PM

Quote:

Originally Posted by linosaurusroot (Post 4832774)
2nd sed command - shouldn't /@/ be /@$/ ?

Why? there is only one @ in the line.

Markus

linosaurusroot 11-19-2012 01:52 PM

Quote:

Originally Posted by markush (Post 4832775)
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.

markush 11-19-2012 01:57 PM

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

GazL 11-19-2012 02:55 PM

I'd do it like this:
Code:

sed 's/\(^[Tt][Hh][Ee][-_ ]\)\(.*\)/\2\t\1\2/' input.list | sort -f | cut -f2

a21210 11-19-2012 04:18 PM

Quote:

Originally Posted by markush (Post 4832671)
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!


All times are GMT -5. The time now is 09:26 AM.