sed and regex issues
Hey guys,
Searched here for this and found lots of answers, but none seems to work :) The case: I have an ini file that lists a lot of settings like this: Code:
SettingA=true So I have tried with sed and regex word boundaries, but it does not work due to some unknown reason. This is what i try to run from inside a script: Code:
sed -i "s/\bSettingC=\b/SettingC=false/g" /path/to/ini_file I have also tried with \<\>, which didn’t work either. Anyone that can enlighten me what im doing wrong here? :) Cheers! R |
It's simple enough if you don't mind needlessly updating a file with the same setting it already had.
Code:
sed -i "s/SettingC=.*/SettingC=false/" /path/to/ini_file Code:
sed -i "/SettingC=false\$/b;s/SettingC=.*/SettingC=false/" [EDIT]And, a bit of testing reveals that sed is not as smart as I thought it was and will replace the file even if no changes were made, so there's no point in adding that test.[/EDIT] In both cases, the regex ends with ".*" so that anything following the equals sign will be replaced. There are lots of things that could be done to make this more robust with regard to whitespace, lines like "MySettingC=whatever", etc. While sed certainly could do all that (It is, after all, a Turing-complete language.), eventually you reach a point where it is just not a suitable language for the task at hand. |
What about the following? It'll only match SettingC= followed by end of line.
Code:
sed 's/SettingC=$/&false/' file |
Thanks for the quick reply rknichols!
With the top example sed -i "s/SettingC=.*/SettingC=false/" /path/to/ini_file It replaces even if the string is "SettingC=" or "SettingC=true" and that seems to be ok and working but not what i was after. The second example you had, did not actually replace at all. Please note that these strings are on single row and they start the row as my example above, so no need to take any of that in to consideration. What im after is to ensure that the string is exactly "SettingC=" and if it, replace with "SettingC=false". I do fully agree with you on right tool for right job and this to me seemed like work for sed. :) Thanks! br, R |
Quote:
|
Quote:
Code:
sed -i "s/SettingC=\$/SettingC=false/" /path/to/ini_file Again, you might want to ignore whitespace, especially at the end of the line: Code:
sed -i "s/SettingC= *\$/SettingC=false/" /path/to/ini_file |
@rknichols: Thanks for explaining and the additional examples.
Thanks for very informative answers both! Cheers, R |
All times are GMT -5. The time now is 02:50 PM. |