1

I have a array looking like this:

array=("(1 2 3) (123)" "2 31 (231)" "4 5 1 (451)" "(te)xt (1234)") 

This array is a example. It does not look like this but its structure is the same (the strings have the same structure).
If I want to use the single strings in a select loop I can do it like this:

select string in "${array[@]}" do # do something done 

But the string in the parentheses is not for display. So I used sed to remove them:

echo "${array[@]}" | sed -r 's/ \([0-9]+\)$//g' 

This did not work. Only the last parentheses were removed and the strings have been merged together. But how can I achieve the desired result which would be the same as the following example:

array2=("(1 2 3)" "2 31" "4 5 1" "(te)xt") select string in "${array2[@]}" do # do something done 
0

    1 Answer 1

    4

    Arrays have no meaning to sed. Once your bash array is passed through sed, becomes plain text.

    Use bash to remove the parenthesis (supposing the part to remove is always at the end of the strings):

    array2=("${array[@]%(*}") 

    (In you bash manual check the section about parameter expansion for more.)

    3
    • This very nice. But if the first part of the string is also in parentheses then the entire string gets emptied.CommentedDec 22, 2013 at 17:01
    • Updated my answer.
      – manatwork
      CommentedDec 22, 2013 at 17:06
    • That's exactly what I was looking for. Thank you!CommentedDec 22, 2013 at 17:41

    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.