What’s the simplest way to do a find and replace for a given input string, say abc, and replace with another string, say XYZ in file /tmp/file.txt?
I am writing an app and using IronPython to execute commands through SSH — but I don’t know Unix that well and don’t know what to look for.
I have heard that Bash, apart from being a command line interface, can be a very powerful scripting language. If this is true, I assume you can perform actions like these.
Can I do it with Bash, and what’s the simplest (one line) script to achieve my goal?
The easiest way is to use
sed(or Perl):which will invoke
sedto do an in-place edit due to the-ioption. The/gflag forsed‘sscommand says to replace globally, i.e. do not substitute only the first occurrence on each input line. This can be called from Bash.(The
-ioption is not standard. On BSD-based platforms, including MacOS, you need an explicit option argument-i ''.)If you really really want to use just Bash, then the following can work:
This loops over each line, doing a substitution, and writing to a temporary file (don’t want to clobber the input). The
mvat the end just moves the temporary file to the original name. (For robustness and security, the temporary file name should not be static or predictable, but let’s not go there.)This uses a Bash-only parameter expansion to perform a replacement on the variable’s value.