Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash script on OS X

Status
Not open for further replies.

santanudas

Technical User
Mar 18, 2002
121
GB
Hi guys,

I was trying to write small bash script, which reads the iTunes’ Library.xml file and then print the name of the songs based on the “given” artist. And I came up with something like this:

Code:
cat ~/Music/iTunes/iTunes\ Music\ Library.xml | tr \\n ' ' | sed 's%</dict>%\n%g' | sed -e 's%<[/[:alnum:]]*>% %g' -e 's/[[:space:]][[:space:]]*/ /g' | grep 'Artist ABBA' | sed -e 's/.*\(file:[[:graph:]]*\).*/\1/g' -e  's%.*/%%g' | sed -e 's/%20/ /g' -e '\,$, s/\.[a-zA-Z0-9]*//' | sort
What is doing is: Reading the file > joining all the lines together > breaking the (putting a new line) after every </dict> (so that all the value between <dict> and <\dict> in a single line now) > grep the Artist name and the associated <Name> value (i.e. song) for every single occurrence.

It's working pretty well under Linux but not on the OS X. Any idea why? Many thanks in advance for helping. Cheers!!
 
Thanks for your replay, JustinEzequiel. I replaced all the \n with \r – that certainly improves the situation a little better but still not quite working. Any other suggestion(s)??
 
what errors are you getting?
if none,
what output did you get
and what output did you expect to get?
 
I copied the "iTunes Music Library.xml" as "Library.xml" to my Linux box and running the script returns these:
Code:
[MacUsers@linux]$ cat Library.xml | tr \\n ' ' | sed 's%</dict>%\n%g' | sed -e 's%<[/[:alnum:]]*>% %g' -e 's/[[:space:]][[:space:]]*/ /g' | grep 'Artist ABBA' | sed -e 's/.*\(file:[[:graph:]]*\).*/\1/g' -e  's%.*/%%g' | sed -e 's/%20/ /g' -e '\,$, s/\.[a-zA-Z0-9]*//' | sort  
Chiquitita
Dancing Queen
Does Your Mother Know
Gimmie Gimmie Gimmie
Honey Honey
I Do I Do I Do I Do
I Have A Dream
Knowing Me Knowing You
Lay All Your Love On Me
......
......
This is exactly what I expect and I get.
Running the same script on my Mac (replacing /n with /r), all I get onlt these:
Code:
macm:~ MacUsers$ cat ~/Music/iTunes/iTunes\ Music\ Library.xml | tr \\r ' ' | sed 's%</dict>%\r%g' | sed -e 's%<[/[:alnum:]]*>% %g' -e 's/[[:space:]][[:space:]]*/ /g' | grep 'Artist ABBA' | sed -e 's/.*\(file:[[:graph:]]*\).*/\1/g' -e  's%.*/%%g' | sed -e 's/%20/ /g' -e '\,$, s/\.[a-zA-Z0-9]*//' | sort 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 Artist ABBA 
 ......
 ......
as far as I can understand, it’s not working mainly because the “joining” part is not working on OS X. I must have been missing something or doing something wrong with the commands on Mac.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top