I have a food.txt file as follows :-mangoes|foo|faa  oranges|foo|faa  chocolates|foo|baz  I am trying to replace foo with bar if condition baz is met. (would like to use a regular expression here b*z )Currently using the sed command below, but this does not directly modify the existing file. I am also not able to use a regular expression (b*z).sed '/baz/s/foo/bar/g' food.txtPlease suggest a way other than sed to modify the existing file directly.   PS : I tried sed -i but I would like to use some other command than sed.I am using mobaxterm (OS - Windows)
Modifying existing file directly to substitute foo with bar ONLY for lines which contain baz
text processing;regular expression
The actual best tool for automated text edits is ex.Although it should be possible to call ex directly, I have found in MobaXterm that I have to call vim -e instead.So, the best way to do this automated edit in MobaXterm (and which will also work on other *nix systems) is:printf '%s\n' 'g/b.*z/s/foo/bar/g' x | vim -es food.txtTo be fully POSIX compliant, it is only necessary to alter it to:printf '%s\n' 'g/b.*z/s/foo/bar/g' x | ex -s food.txtHowever, calling the ex command may not work correctly on MobaXterm (it doesn't on my installation.)  Try the vim -es version of the command if the ex version fails.