0

for a single replace the curly brace works fine
but i cant find a solution for multiple replace where is the trick ?
or its not possible
for multiple replace i use sed and for single the curly braces method

#example vars
addons="WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL READ_SDT_CHARSETS WITH_DEBUG MODULE_MONITOR"
protocols="MODULE_hans MODULE_paul MODULE_klaus MODULE_franz"
readers="READER_foo READER_bar READER_foobar READER_barfoo"
card_readers="CARDREADER_type1 CARDREADER_type2 CARDREADER_type3"

_create_module_arrays(){
i=0;
for e in $(echo "$addons" | sed 's/WEBIF_//g;s/WITH_//g;s/MODULE_//g;s/HAVE_//g;s/_CHARSETS//g;);
do
<------>SHORT_ADDONS+=($e);
<------>SHORT_MODULENAMES+=($e);
done;
for e in ${protocols//MODULE_/};
do
<------>SHORT_PROTOCOLS+=($e);
<------>SHORT_MODULENAMES+=($e);
done;
for e in ${readers//READER_/};
do
<------>SHORT_READERS+=($e);
<------>SHORT_MODULENAMES+=($e);
done;
for e in ${card_readers//CARDREADER_/};
do
<------>SHORT_CARD_READERS+=($e);
<------>SHORT_MODULENAMES+=($e);
done;
for e in $addons $protocols $readers $card_readers;
do
<------>ALL_MODULES_LONG+=($e);
done;
for e in "${SHORT_MODULENAMES[@]}";
do
<------>INTERNAL_MODULES["$e"]="${ALL_MODULES_LONG[i]}";((i++));
done;
};
  • You're already using the form ${variable//pattern/replacement}, which should replace all matches. Please edit the question to show your input (ie. the contents of $protocols, $readers etc.), expected results, and actual results. – JigglyNaga May 29 '16 at 14:45
  • look @ line 3 for e in $(echo "$addons" | sed – Gorgone Impertinence May 29 '16 at 15:18
  • The protocols, readers and card_readers variables aren't set on line 3 or anywhere else. We need to see their values to work out why bash's pattern substitution isn't doing what you want. – JigglyNaga May 29 '16 at 15:45
  • i add vars in first post ... but is clear what i sed in the first for loop – Gorgone Impertinence May 29 '16 at 15:53
  • Oh, I see - you want to use bash instead of that sed command. Is there a reason you can't use multiple steps with temporary variables, ie ADD=${addons//WEBIF_/} ; ADD=${ADD//WITH_/} ; ... ? – JigglyNaga May 29 '16 at 16:37
2

In bash, if you want to delete these patterns, you could use extended pattern matching:

# optional - save extglob state
$ shopt -q extglob && extglob_disabled=false || extglob_disabled=true

# enable extended pattern matching
$ shopt -s extglob

$ addons="WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL READ_SDT_CHARSETS WITH_DEBUG MODULE_MONITOR"
$ echo ${addons//@(WEBIF_|WITH_|MODULE_|HAVE_|_CHARSETS)}
LIVELOG JQUERY TOUCH SSL READ_SDT DEBUG MONITOR

# optional - restore extglob state
$ $extglob_disabled && shopt -u extglob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.