3

I have some string that read like this one here

Dio - We Rock-Greatest Hits (2CD) (2004)

I use this

NewDirectoryName=${NewDirectoryName%'(" ")'* }
NewDirectoryName=${NewDirectoryName//[^A-Za-z ]/ }

and it gives me this left over :

NewDirectoryName is -> Dio We Rock Greatest Hits Cd

it removes the (2CD) and (2004) alltogether leaving me with Cd.

What puzzles me is if it removes the (2004), then why does it turn (2CD) into Cd. Why only remove (2 and the ) and why is it changing D to d?

How do I get rid of the entire thing to get this:

Dio We Rock Greatest Hits
3
  • 1
    Your first line does nothing. Is there a typo in it? Is the first lower-case z in [^A-za-z ] a typo? It should be an upper-case Z Commented Nov 25, 2015 at 14:22
  • yep type O should read [^A-Za-z ] I'll fix that thanks Commented Nov 25, 2015 at 14:39
  • youv'e got inner quotes in your '(" ")' pattern, but you don't have such in your match. and so you don't match. do ${var%%\(*} to strip everything up to the first ( character. Commented Nov 25, 2015 at 14:44

1 Answer 1

4

As I commented, your first expansion does nothing. The literal string (" ") is not in the string so nothing is removed. Then then 2nd expansion removes all characters that are not a letter or a space. That's why "CD" stays. There's nothing in your code that would lower-case the "d" in "CD".

If you want to remove all bits in parentheses:

shopt -s extglob
NewDirectoryName='Dio - We Rock-Greatest Hits (2CD) (2004)'

NewDirectoryName=${NewDirectoryName//\(*([^\)])\)}
# ...................................aa........cc
# .....................................bbbbbbbb
# (a) a literal open parenthesis
# (b) zero or more non-close-parenthesis characters
# (c) a literal close parenthesis

echo ">$NewDirectoryName<"
>Dio - We Rock-Greatest Hits  <

To remove trailing whitespace:

NewDirectoryName=${NewDirectoryName%%+([[:blank:]])}

This uses "extended pattern matching" patterns, documented at the end of this section of the manual: https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching

8
  • is there any advantage to using extended globs here? Commented Nov 25, 2015 at 15:00
  • You can't do "one or more non-whatever" without them -- glob patterns aren't that sophisticated Commented Nov 25, 2015 at 15:02
  • i dont follow. what one or more non whatevers are you looking for? Commented Nov 25, 2015 at 15:26
  • "zero or more non-close-parenthesis characters" Commented Nov 25, 2015 at 15:31
  • but the asker says how can I get rid of everything in ()? how does a name without them factor? im definitely missing something. sorry about that. Commented Nov 25, 2015 at 15:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.