1

I've got a very simple bash script which I'm passing values to

I want to strip the prefix from the value passed to the script.

The works and strips test- from the passed value..

IN=$1
arrIN=(${IN//test-/})
echo $arrIN

So test-12345 returns 12345

Is there anyway to amend this so it will remove either test- or local- ?

I've tried :

arrIN=(${IN//test-|local-/})

But that didn't work..

Thanks

1
  • ${var##*-} gets the portion from last - to the end of the string. But you are using array notation, so it is not clear if this is enough/ Commented Dec 3, 2014 at 12:42

4 Answers 4

1

Try using sed as below:

IN=$1
arrIN=$(echo $IN | sed -r 's/test-|local-//g')
echo $arrIN

Here sed will search for "test-" or "local-" and remove them completely anywhere in the whole input.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I've gone for this answer as I understand it easier.. When I look back at this in 6 months, I'll know what it does :)
1

If you want to change "test-" or "local-" to "", you could use a command like this:

awk '{gsub(/test-|local-/, ""); print}'

Comments

1

You can use sed and get the exact result

IN=$1
arrIN=$( echo $IN | sed 's/[^-]\+.//')
echo $arrIN

Comments

1

You can do it with extglob activated:

shopt -s extglob
arrIN=(${IN//+(test-|local-)/})

From man bash:

  ?(pattern-list)  
         Matches zero or one occurrence of the given patterns  
  *(pattern-list)  
         Matches zero or more occurrences of the given patterns  
  +(pattern-list)  
         Matches one or more occurrences of the given patterns  
  @(pattern-list)  
         Matches one of the given patterns  
  !(pattern-list)  
         Matches anything except one of the given patterns 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.