4

I have to change a properties file containing the property:

ro.product.firmware=0.0.1 

with a new value that is coming from a function called in a different section of my bash script. I cannot get the regex to work properly. For this particular case I need the value to be changed from 0.0.1 to $1 but the value will not always be 0.0.1. The regex I currently have is:

sed -i 's/^(ro\.product\.firmware).*$/(ro\.product\.firmware="$1")'
5
  • What regex did you try?
    – shivams
    Commented May 28, 2015 at 19:19
  • sed -i 's/^(ro\.product\.firmware).*$/(ro\.product\.firmware="$1")'
    – John
    Commented May 28, 2015 at 19:24
  • Edit your question and include this regex in it. And please read the guidelines for framing a better question, lest your question will be downvoted by other users.
    – shivams
    Commented May 28, 2015 at 19:25
  • my apologies, this is my first experience using a professional forum.
    – John
    Commented May 28, 2015 at 19:29
  • It's okay. However, it would be good to read atleast the formatting guidelines here unix.stackexchange.com/editing-help so that you format your questions better the next time.
    – shivams
    Commented May 28, 2015 at 19:35

2 Answers 2

5

This should work for your case:

sed -ri 's/^(ro\.product\.firmware\=)(.*)$/\1'"$1"'/g' file.txt

Here,

-r ==> for using extended regex
\1 ==> for the first captured group
3
  • This is very close except it gives me "$1" instead of the value of the variable
    – John
    Commented May 28, 2015 at 19:43
  • Okay. It has been edited. Now it should work fine.
    – shivams
    Commented May 28, 2015 at 19:46
  • Thank you for the help, I greatly appreciate the answer. sed -ri 's/^(ro\.product\.firmware\=)(.*)$/\1'"$1"'/g' was what I needed in order to print the variable
    – John
    Commented May 28, 2015 at 19:46
1
#!/bin/bash

#some logic

export value=$(some_function)
#note the export

#more code

perl -ie 's/^ro\.product\.firmware.*$/ro.product.firmware=$ENV{value};'

#yada yada yada

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .