1

I have a file called app.properties like this

prop1.value=hi
prop2.value=hello
prop3.url=https://google.com

As per my requirement, If I see any .value , i need to call an utility function to get the value replaced. just for the example the utility simply multiples the number of chars passed to it * 2.

So, it should become like this.

prop1.value=4
prop2.value=10
prop3.url=https://google.com

I have done simple find and replacement with sed. Not sure how to call any command to replace the value.

  sed -E 's/.value=(.*)/.value=\1/g' app.properties

If sed is not correct choice, can you suggest any other alternative?

1 Answer 1

1

How about

$ awk 'BEGIN{OFS=FS="="} $1 ~ /\.value$/ {$2 = 2 * length($2)} 1' app.properties 
prop1.value=4
prop2.value=10
prop3.url=https://google.com

If you want something closer to your sed approach, then perhaps

perl -pe 's/\.value=(.*)/sprintf ".value=%s", 2 * length $1/e' app.properties

or

perl -pe 's/(?<=\.value=).*/2 * length $&/e' app.properties
2

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.