LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Q: How to remove brackets from mp3 files? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-q-how-to-remove-brackets-from-mp3-files-927934/)

vinodub 02-06-2012 01:23 PM

Bash Q: How to remove brackets from mp3 files?
 
Hi,

I am newb to shell scripting and i wanted to remove brackets from my mp3 files.

The format is Abc [abc].mp3
I need Abc.mp3

i know that i can use either regex or sed to do this.....but not able to come up with solution.
If you can explain a bit about your solution...it will help me learn scripting.
Thanks.

Tinkster 02-06-2012 02:11 PM

Hi, welcome to LQ!

Code:

echo 'Abc [abc].mp3'|sed -r 's/^([^][]+) \[.+\](\..*)$/\1\2/'
Abc.mp3


Search (and remember) for all characters from beginning of line that aren't brackets:
Code:

^([^][]+)
Got space and something enclosed in brackets?
Code:

\[.+\]

Find & remember everything from the dot to the end of line:
Code:

(\..*)$

Replace the whole line with the two remembered bits:
Code:

\1\2


Cheers,
Tink


P.S.: sed or regex makes little sense, in sed you commonly heavily rely on regex.

vinodub 02-06-2012 05:05 PM

Thanks Tinkster.
 
I never want turn-key answers....it hinders development of skills.

Tinkster 02-06-2012 05:31 PM

Quote:

I never want turn-key answers....it hinders development of skills.
Good .... that statement, btw, is my signature, and not aimed at anyone in particular.
Just thought I'd clarify that. ;}



Cheers,
Tink

vinodub 02-07-2012 12:28 PM

is this correct?
 
^([^][]+)

i understood your solution except for the above part.

correct me if i got this wrong..... the () tells linux to save this in memory. the starting ^ indicates the beginning of the line...

does [^] say anything other than and []+ say one or more brackets? i need some work on character sets....

Tinkster 02-07-2012 02:11 PM

Quote:

Originally Posted by vinodub (Post 4596227)
^([^][]+)

i understood your solution except for the above part.

correct me if i got this wrong..... the () tells linux to save this in memory. the starting ^ indicates the beginning of the line...

does [^] say anything other than and []+ say one or more brackets? i need some work on character sets....

Let's dissect this one:
Code:

^([^][]+)
^^^^      ^
|  |not in class
|
|
|
| |character class
||------|--- remember
Beginning of line

So ... the first one you correctly interpreted as beginning of line,
and the parenthesis indeed mark what's to be remembered.
[^][] is a character class of all characters that aren't a ] or a [,
and the + is simply "more than one of anything not a [ or ]".


HIH



Cheers,
Tink

vinodub 02-07-2012 03:42 PM

Beautiful.
 
Thanks Tinkster. Understood everything.


All times are GMT -5. The time now is 07:40 PM.