I need a bash script which should do the following: - read from an input file line by line which having format: [Environment]=[file name]=[property key]=[property value] - it will modify all files by replacing all properties in a $SOURCE directory according to the input file. Now I'm stuck at the point "replace and count".
The code until the "replace and count" phase:
PROPERTIES_LIST=/home/user/test_scripts/envPropList.txt
SOURCE=/home/user/test_scripts/source-directory
PROPERTIES_LOCATION=/WEB-INF/classes/
PROPERTIES_SOURCE=$SOURCE$PROPERTIES_LOCATION
ENV=$1
echo "> Update for ENVIRONMENT: $ENV... Please wait!"
NR=1
while read line; do
IFS== read env prop <<< "$line"
if [[ $env == $ENV]]
then
IFS== read file key value <<< "$prop"
if [[ -z $file ]] || [[ -z $key ]] || [[ -z $value ]]
then
echo "> [ERROR] [LINE $NR] - WRONG/MISSING PROPERTY: $line"
else
//MODIFY $file BY REPLACE AND COUNT
//IF $key IS NOT FOUND AT ALL AN ERROR MESSAGE SHOULD BE DISPLAYED. ITERATION SHOULD CONTINUE
//IF $key IS FOUND MORE THEN ONCE, A WARNING MESSAGE SHOULD BE DISPLAYED. ITERATION SHOULD CONTINUE
echo "done"
fi
fi
NR=$(( $NR + 1 ))
done <$PROPERTIES_LIST
I have tried the following without success without success because value in properties can be any character (like: &,/,....):
COUNT=$(grep -c "$key" $PROPERTIES_SOURCE$file)
sed -i "s/${OLD}/${NEW}/g" $PROPERTIES_SOURCE$file
Also awk didn't worked as expected:
DEST=/home/user/test_scripts/test.txt
OLD='asd.asd'
NEW='test/test?test.test&test=test'
COUNT=$(grep -c "$OLD" $DEST)
#sed -i "s/#${OLD}#/#${NEW}#p/g" $DEST
#echo "$OLD=$NEW"
echo "nr de rezultate: "$COUNT
awk -v OLD=$OLD -v NEW=$NEW '
($0 ~ OLD) {gsub(OLD, NEW); count++}1
END{print count " substitutions occured."}
' "$DEST"
And for input file:
asd.asd
ewrqfg
qweasd.asdqwreqe
asd asd.asd
egd
test
I have the following output:
test/test?test.testasd.asdtest=test
ewrqfg
qwetest/test?test.testasd.asdtest=testqwreqe
test/test?test.testasd asdtest=test.asd
egd
test
If I remove "&" from $NEW, everything goes fine.