What I am trying to do is: There's a file, inside of it, there's a line (it surely will be there, 100%) in this model:


Where the value is numerical and unknown by me because it changes between device and device... I need to, somehow, get the xxx value and replace it with another value given by the user via a TextField (I'm using Java for this, it's for an Android application, but it's pure shell script anyway).

Is valid:

  1. Find and delete the whole line and replace with the full line e.g delete name_of_the_value=xxx and replace it with name_of_the_value=user_value
  2. Replace only the xxx (value) part
  3. Comment with '#' any existing matching lines and write the full line at the bottom of the file

But how can I use echo, sed or awk if I don't know the value?

  • Tried to reorganize the layout a bit, but what I don't get: what do you mean by "Is valid:". Furthermore, what follows then, should it all be done, or either one of them? I don't fully understand. – Jacob Vlijm May 9 '16 at 10:58
  • "is valid" in this case, means something that can be done without compromising system, anyone if works can be done, not all of them! I just don't know how to.... :( – Lennoard Silva May 9 '16 at 11:07
  • It's not at all uncommon - in fact there's a whole regular expression language to deal with exactly that. If you want help constructing a suitable expression you will need to be specific about what xxx is and what you want to change it to i.e. a sample of your input file and desired output file. – steeldriver May 9 '16 at 11:16

Assuming the part for user_value is always 3 characters long you can do it like this:

sed "s/name_of_the_value=.{0,3}/name_of_the_value=$new_value/"

Or if you be sure there is nothing behind in that line you aswell could use:

sed "s/name_of_the_value=.*/name_of_the_value=$new_value/"

I hope i got that REGEX right, I'm no pro in this. So the value should be all the time 3 characters long best in hex format because decimal doesn't be always 3 characters long in cases below 100. The second case should work even i the number is below 100 but it assumes the whole name_of_the_value=user_value part is in a line of its own. Now testing this in terminal the output looks like this for the second case:

video@NAUTH:~$ echo "name_of_the_value=xxx" | sed "s/name_of_the_value=.*/name_of_the_value=$USER/"

Of course you need to pipe the output back into the file, or use a temporary file to swap around. You could use:

sed "s/name_of_the_value=.*/name_of_the_value=$new_value/" /input_file | sponge /input_file

Sed only changes the output while reading the file, not writing it, sponge saves the output then until you are through and writes the changes back.

If you don't want to or can't use sponge write in a second file and then switch them like this chain of commands.

sed "s/name_of_the_value=.*/name_of_the_value=$new_value/" /input_file > /output_file
rm -v /input_file
mv -v /output_file /input_file

As @kos pointed out in the comment you can as well use the -i flag of the sed command to write back into the input file without the use of a temporary file or sponge:

sed -i "s/name_of_the_value=.*/name_of_the_value=$new_value/" /input_file
  • yes it's always 3 characters, I will check out your code and put together with my Java code (I'm not getting the user value through the shell) and soon I will post here the results as well as the whole code. – Lennoard Silva May 9 '16 at 12:37
  • I couldn't write the file. I tried sed "s/ro.sf.lcd_density=.*/ro.sf.lcd_density=test_value/" /system/build.prop there's no need to "echo" because this line already exists in the file /system/build.prop but nothing happens, when something happens, it shows me the full content of the file and the last line is changed with "test_value" but if I check the file, it's intact – Lennoard Silva May 9 '16 at 13:51
  • you need to pipe the output back into the file, you can do that with sed "s/ro.sf.lcd_density=.*/ro.sf.lcd_density=test_value/" /system/build.prop | sponge /system/build.prop. sed only changes the output while reading the file, not writing it, sponge saves the output then until you through and writes the changes back. if you don't want to use sponge write in a second file and then switch them. – Videonauth May 9 '16 at 13:57
  • Sadly, sponge wasn't recognized by the terminal, maybe this binary is missing. I'm dealing with Android and as well as you, I expected that it'd work but it didn't. About the second suggestion, please explain more, I'm really new into this... Thanks for your time – Lennoard Silva May 9 '16 at 14:48
  • Extended my answer to accommodate this discussion. – Videonauth May 9 '16 at 15:02

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.