Search code examples
bashreplacescriptingironpython

Find and Replace Inside a Text File from a Bash Command


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?


Solution

  • The easiest way is to use sed (or Perl):

    sed -i -e 's/abc/XYZ/g' /tmp/file.txt
    

    which will invoke sed to do an in-place edit due to the -i option. The /g flag for sed's s command says to replace globally, i.e. do not substitute only the first occurrence on each input line. This can be called from Bash.

    (The -i option 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:

    while IFS='' read -r a; do
        echo "${a//abc/XYZ}"
    done < /tmp/file.txt > /tmp/file.txt.t
    mv /tmp/file.txt{.t,}
    

    This loops over each line, doing a substitution, and writing to a temporary file (don't want to clobber the input). The mv at 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.