LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-06-2005, 09:13 AM   #1
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Rep: Reputation: 36
bash+awk to fix MP3 tags


I'm trying to write a small shellscript to fix my MP3 tags, and thanks to Google I've concluded that bash+awk is the way to go.

All my music is in the format 01 - Bruce Springsteen - Born to Run.mp3 (track number - artist - track).
In addition, the mp3 files are always stored in a directory with the album name, so just based on the filename and location, I should be able to get

ARTIST
ALBUM
TRACK
NUMBER

However, my programming capabilities are to say the least rusty, so even my small attempt to start fails with a crude error (Invalid pointer):

Code:
!/bin/sh
CURDIR=`pwd`
ALBUM=`basename "$CURDIR"`
echo $ALBUM

for file in *.mp3 ; do
  echo $file
  awk -F"-" '{ print $1 }' $file
done
Could anyone kindly point me in the right direction for how to get the track number (awk print $1), artist ($2) and track $3 into the right variables so I can then pass it on to id3tag to strip all v1 tags and properly set the v2 tags?

Thanks in advance for any insight

-Y1
 
Old 11-06-2005, 12:16 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You're quite close :}

Code:
track=`awk -F"-" '{ print $1 }' $file`
artist=`awk -F"-" '{ print $2 }' $file`
title=`awk -F"-" '{ print $3 }' $file`

Cheers,
Tink
 
Old 11-06-2005, 01:42 PM   #3
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Original Poster
Rep: Reputation: 36
Hi Tinkster,

Thanks much for your answer!

I tried your suggestion and got the following script:
Code:
#!/bin/sh
CURDIR=`pwd`
ALBUM=`basename "$CURDIR"`
echo $ALBUM

for file in *.mp3 ; do
  TRACK=`awk -F"-" '{ print $1 }' $file`
  ARTIST=`awk -F"-" '{ print $2 }' $file`
  TITLE=`awk -F"-" '{ print $3 }' $file`
  echo "Artist: $ARTIST\nAlbum: $ALBUM\nTitle: $TITLE\nTrack: $TRACK"
done
However, it only gives me:
*** glibc detected *** free(): invalid pointer: 0x08093340 ***
*** glibc detected *** free(): invalid pointer: 0x08093340 ***
*** glibc detected *** free(): invalid pointer: 0x08093340 ***
Artist: \nAlbum: Echo\nTitle: \nTrack:

Is there a bug in awk, or am I still missing a vital part?

Thanks!
 
Old 11-07-2005, 12:13 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What do you get if you run it from a command-line?

e.g.
ls -1 "01 - Bruce Springsteen - Born to Run.mp3"|awk -F"-" '{ print $1 }'
Similar messages?


Cheers,
Tink
 
Old 11-07-2005, 01:03 AM   #5
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Original Poster
Rep: Reputation: 36
Quote:
Originally posted by Tinkster
What do you get if you run it from a command-line?

e.g.
ls -1 "01 - Bruce Springsteen - Born to Run.mp3"|awk -F"-" '{ print $1 }'
That seems to work just fine - the above command prints
01\

And ls -1 "01 - Bruce Springsteen - Born to Run.mp3"|awk -F"-" '{ print $2 }' gives
\ Bruce Springsteen\

I wonder why it fails in the script though... I'm on Slackware 10.2 - could it be an awk problem?

Also - do you have any suggestions on how to cut off the trailing spaces (\ \ ) ?

Thanks again for your patient assistance!

-Y1
 
Old 11-07-2005, 02:41 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally posted by Yalla-One
Code:
#!/bin/sh
CURDIR=`pwd`
ALBUM=`basename "$CURDIR"`
echo $ALBUM

for file in *.mp3 ; do
  TRACK=`awk -F"-" '{ print $1 }' $file`
  ARTIST=`awk -F"-" '{ print $2 }' $file`
  TITLE=`awk -F"-" '{ print $3 }' $file`
  echo "Artist: $ARTIST\nAlbum: $ALBUM\nTitle: $TITLE\nTrack: $TRACK"
done
i think 'awk' is trying to open '01 - Bruce Springsteen - Born to Run.mp3' and separate any column that is separated with a '-' and print the first column.

i think you should 'ls -1 > track-list.txt' then do a 'for line in `cat track-list.txt`...'

hope this helps,
 
Old 11-09-2005, 04:19 AM   #7
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
#!/bin/bash

ALBUM=`basename $PWD`
dash="----------------------------"

echo "$dash List of mp3 files in $PWD $dash"
ls -l *.mp3

for i in *.mp3 ; do
        echo "$dash $i $dash"
        OLDIFS=$IFS ; IFS=-
        set `echo "$i" | sed -e 's/ *- */-/g'`
        IFS=$OLDIFS
        echo -e "Artist: $2\nAlbum : $ALBUM\nTitle : ${3%%\.mp3} \nTrack#: $1"
done
HTH
 
Old 11-12-2005, 09:11 AM   #8
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Original Poster
Rep: Reputation: 36
That did the trick - thanks much!

Just needed one small adjustment due to spaces in the filename, which was fixed by adding "" around $PWD to get the correct name of the album.

Thanks much to everyone who helped!

-Y1
 
  


Reply



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
Apache: show mp3 id tags in directory browsing? wijnands Linux - Software 1 08-09-2005 07:09 AM
Updating ID3 (MP3) Tags DJ747 Linux - Software 3 05-15-2005 11:22 AM
rearrange folder structure using mp3 tags roemisch Linux - Software 1 04-18-2005 10:15 AM
mp3 id tags... how to change them ? miko3k Linux - Software 7 01-31-2005 07:47 AM
mp3 tags nautilus_1987 Linux - Software 5 10-27-2002 11:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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