So I have a configuration file that is parsed by various scripts, therefore the format can't be changed, although the contents can as long as the format is being strictly followed. This file by default contains strings such as
multiconfig:nmb-devel:nmb-trs-devel
multiconfig:nmb-deploy:nmb-trs-deploy
multiconfig:nmb-deploy:nmb-trs-deploy+
multiconfig:ijk-devel:ijk-trs-devel
multiconfig:ijk-deploy:ijk-trs-deploy
multiconfig:ijk-deploy:ijk-trs-deploy+
multiconfig:qrs-devel:qrs-trs-devel
multiconfig:qrs-deploy:qrs-trs-deploy
multiconfig:qrs-deploy:qrs-trs-deploy+
Currently, I have a script that parses these configurations (multiconfig:...) into an array, as well as an array of configurations to replace these original configurations. e.g.
disregard the following tail operation, the ACTUAL conf.txt contains another match to be skipped, which is what is successfully done.
TARGETS="multiconfig:new-devel:new-trs-devel multiconfig:newer-devel:newer-trs-devel multiconfig:newest-devel:newest-trs-devel"
NEW_TARGETS_ARR=( $TARGETS )
OLD_TARGETS_ARR=($(sed -n '/multiconfig/p' conf.txt | tail -n +2 | awk '!seen[$0]++'))
Note: Not the sed operation in question
These work fine, and result in proper arrays such as:
NEW_TARGETS_ARR: multiconfig:new-devel:new-trs-devel, multiconfig:newer-devel:newer-trs-devel, multiconfig:newest-devel:newest-trs-devel
OLD_TARGETS_ARR: multiconfig:nmb-devel:nmb-trs-devel, multiconfig:nmb-deploy:nmb-trs-deploy, multiconfig:nmb-deploy:nmb-trs-deploy+, multiconfig:ijk-devel:ijk-trs-devel, multiconfig:ijk-deploy:ijk-trs-deploy, multiconfig:ijk-deploy:ijk-trs-deploy+, multiconfig:qrs-devel:qrs-trs-devel, multiconfig:qrs-deploy:qrs-trs-deploy, multiconfig:qrs-deploy:qrs-trs-deploy+
My objective is to replace the old configurations by the new configurations.
My method of doing this right now is by looping through the OLD_TARGETS_ARR and replacing each OLD_TARGETS_ARR[index] with the NEW_TARGETS_ARR[index] as such:
for i in ${!OLD_TARGETS_ARR[@]}
do
if [ "${NEW_TARGETS_ARR[$i]}" = "" ]; then
NEW_TARGETS_ARR[$i]="[EMPTY]"
fi
echo $i
echo "OLD TARGET $i: ${OLD_TARGETS_ARR[$i]}"
echo "NEW TARGET $i: ${NEW_TARGETS_ARR[$i]}"
echo "sed -i \"s/${OLD_TARGETS_ARR[$i]}/${NEW_TARGETS_ARR[$i]}/g\" conf.txt"
sed -i "s/${OLD_TARGETS_ARR[$i]}/${NEW_TARGETS_ARR[$i]}/g" conf.txt
done
Now, theoretically what this should (or what I want) result in is as follows
conf.txt
multiconfig:new-devel:new-trs-devel
multiconfig:newer-devel:newer-trs-devel
multiconfig:newest-devel:newest-trs-devel
[EMPTY]
[EMPTY]
[EMPTY]
[EMPTY]
[EMPTY]
[EMPTY]
Although what this actually results in is as follows
conf.txt
multiconfig:new-devel:new-trs-devel
[EMPTY]
[EMPTY]+
[EMPTY]
[EMPTY]
[EMPTY]+
[EMPTY]
[EMPTY]
[EMPTY]+
While I haven't figured out why more entries than are necessary are getting replaced with '[EMPTY]', I have figured out that on the '+' on 'deploy+' is not getting parsed, and must be escaped in some way, shape, or form. Given that these sed operations are dynamic, I can't simply add a \ in front of the '+'. First, how could I ensure that the '+' is getting parsed as part of the string to search for in the sed operation?
Second, I probably am misunderstanding some basic part of sed, or my loop, although why is everything except for the first match getting replaced with '[EMPTY]'?
I appreciate any input anyone can give,
thank you all in advance!