I have the following Java properties file that is maintained by humans.# file.input = /very/old/name# file.input = /old/namefile.input = /current/namefile.input.default = /default/nameother.file.input = /current/other/name# Other commentsother.properties = must stay the sameI must create a script that will edit this file without touching at all at the rest of the files. Therefore I cannot dump new properties in the config file.Humans will often copy/paste a line and comment it for further use. My application must edit exclusively config file without touching any of the comments.In this particular case, I want to edit the line file.input to set the value to /new/name without any commented line being touched and without any other properties being touched as well (file.input.default, other.file.input). My script will not keep the current line for history purpose like humans usually do. Also, I don't know the current value set to the key file.input.Where am I stuck? At the very beginning. I'm not a Bash expert, I rarely use it except to call commands in sequence and I use some ifs sometimes. But that's all.I could use perl to use regular expressions, but perl is apparently not installed on this system. So I turned to sed, in turn, but I don't get any of this: it seems I can only replace a specific value to another. I could wrap that in the reading of the file, and rewrite it except for the expected line that I could change. Also, I would barely be able to handle human-added spaces in front of the property I want to change.So if you have an alternative please share it, because I start to become alienated.
Editing a Java properties file with Bash
bash;shell script;text processing
Using the -i option of sed (which edits inplace). The caveat is the character used to delimiter the substitution command must not appear in the NEW string. The example uses ':' rather than '/' to delimit (s:old_string:new_string:)NEW=/new/name    sed -i 's:^[ \t]*file.input[ \t]*=\([ \t]*.*\)$:file.input = '${NEW}':' f