LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-08-2007, 08:29 PM   #1
Shay
Member
 
Registered: Sep 2003
Posts: 77

Rep: Reputation: 15
Bash to sort ogg files by internal artist, album info


I've got a pile of ogg files in a folder. I would like to write a bash script to separate the files into artist and album folders. Each file contains strings that look like:
artist=some artist^
album=some album^

I thought there might be a way to use bash and perl to discover the album names, create folders, and move the files into those folders.

I think if I get past the first step, I can get the rest.
grep 'album=' *ogg doesn't print the line containing album=some album^
Instead, I get this:
Binary file song1.ogg matches
Binary file song2.ogg matches
Binary file song3.ogg matches

That would be useful enough to me if I could just generate a list of all of the album names before I start.

Any help appreciated.
 
Old 05-09-2007, 12:24 AM   #2
Sonneteer
Member
 
Registered: May 2006
Location: Canada
Distribution: Slackware 13.37; Ubuntu 12.04
Posts: 81
Blog Entries: 5

Rep: Reputation: 15
Instead of greping the files directly, use ogginfo on the files, and pipe the output of that to grep.
Code:
ogginfo *.ogg | grep album
Something like this would be able to give you a list of all the albums:
Code:
ogginfo *.ogg | grep album | uniq | cut -d = -f 2
 
Old 05-09-2007, 12:30 AM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Go with soneteer's solution. But for further background, the following will apply to other files which have a mixture of ASCII characters and binary data. (I didn't know about ogginfo, and typed the following before I saw his response.)

ogg files contain not only printable ASCII characters, but also binary data. grep usually works with printable ASCII data, line by line. With binary data, there is no such thing as a "line" in the ASCII sense, and what would be the line termination character (LF) in ASCII data can appear anywhere in plain binary data, and that byte means whatever the binary usage would dictate.

So grep goes out of its way to detect a binary file, and just let you know whether the specified string is within that file.

You have two ways to fix this: the wrong way, and the right way.

The wrong way, with which you may wish to experiment if you have nothing better to do, is this:

Code:
grep -a 'album=' *ogg
That will ignore the possibility that the file might contain binary data (which is the case with ogg files). What you really want to do, though, is consider just the ASCII parts of each file. You can pipe each file through the strings command, and send the output through grep. Try this:

Code:
for xxx in *ogg
do
  strings $xxx | grep 'album=' | sed -e "s/^/$xxx:/"
done
Note that the first occurrence of xxx is not preceded by a dollar sign. Also notice the double quotes around the sed parameter; using single quotes would prevent the $xxx from being interpreted properly. With double quotes, the $xxx will get changed into the name of the individual ogg file; with single quotes, it will remain $xxx, and that's what you'll see at the beginning of each line of output.

For more information:

Code:
man grep
man strings
man sed
Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 05-09-2007 at 12:35 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to sort image files dtcs Programming 5 09-26-2006 09:50 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 11:07 AM
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 03:04 AM
Grip album info not appearing skidmark41 Linux - Software 1 12-02-2004 04:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:07 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
Open Source Consulting | Domain Registration