1

I have a file values.properties which contain data, like:

$ABC=10 $XYZ=20 

I want to create a shell script that will take each element one by one from above file.

Say $ABC, then go to file ABC.txt & replace the value of $ABC with 10. Similarly, then go to file XYZ.txt and replace $XYZ with 20.

1 Answer 1

1

I think maybe this should be in the Unix and Linux section, the solution I've hacked together is as follows:

cat values.properties | grep "=" | cut -d "$" -f2 | awk -F "=" '{print "s/$"$1"/"$2"/g "$1".txt"}' | xargs -n2 sed -i 

The flow is like so:

  1. Filter out all the value assignments via: grep "="
  2. Remove the '$' via: cut -d "$" -f2
  3. Use awk to split the variable name and value and construct sed replacement command
  4. Use xargs to pull in the replacement parameter and target file via: xargs -n2
  5. Finally pass sed to as the command to xargs: xargs -n2 sed
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted, good hack. This will work for variables with naming convention $ABCD in the destination file, but will not work for ${ABCD}. Kindly update the command to cover both scenario.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.