You are not logged in.

#1 2011-07-26 23:03:25

cyberius
Member
From: Germany
Registered: 2008-08-30
Posts: 97

[solved] Need a little help with sed and regular expressions

Hello!

I am shure this is something easy for most of you smile

I want to make a script, which converts filenames of my ripped MP3s (replaces '_' with spaces, removes leading track numbers...)

But I have some problems:

j=$(echo $j | sed 's/_\+/ /g') 
j=$(echo $j | sed 's/^[0-9]{0,3}//g')
j=$(echo $j | sed 's/[^ ]-[^ ]/ - /g')

j=$(echo $j | sed 's/_\+/ /g') << this is working fine (converts all "_" to spaces)

j=$(echo $j | sed 's/^[0-9]{0,3}//g') << is NOT working, why??
For Example in "01-somebody_feat_someone-somemusic.mp3" the leading "01" number is NOT being removed..

j=$(echo $j | sed 's/[^ ]-[^ ]/ - /g') << how can I insert spaces before and after the "-"?
So that "someone-somemusic" becomes "someone - somemusic" (but only where "-" is surrounded by letters)

Last edited by cyberius (2011-07-27 18:50:54)

Offline

#2 2011-07-26 23:07:14

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: [solved] Need a little help with sed and regular expressions

For sed, you must escape { and } to use them as you want (just slap a \ before them).

For the last expression, capture the letter before/after the dash -- use \( and \) -- and then substitute it for something like "\1 -" and then "- \1". You'll want to split this into two pieces, one for the front and one for the back so you can get "somemusic -someband" the way you want without a bunch of cases.
Edit: Or, you could just do a replace for "-" to be " - " and then have another expression to reduce spaces. I see you've used \+ before, so I'm guessing you can figure that out wink

Also, sed has the -e switch so you can do multiple different expressions with one invocation.

Also (also), have you looked into something like Picard with automatic track renaming? You can even customize how they are renamed.

Edit (2): Also^3, check out prename. There are different versions, ones which use PCRE and ones that use other standards, but it is for renaming files based on regular expressions, which is what you're doing. In any case, you might want to put you script into the User made scripts thread when you feel more comfortable and get some more critiquing, if you're interested.

Last edited by jac (2011-07-26 23:13:27)

Offline

#3 2011-07-27 18:52:54

cyberius
Member
From: Germany
Registered: 2008-08-30
Posts: 97

Re: [solved] Need a little help with sed and regular expressions

Thank you very much for your suggestions!

I will put it into the scripts thread, when I am finished.

Offline

Board footer

Powered by FluxBB