A Shell Tricks: Search and Replace
It's easy to use VS Code to search and replace text entries. Yet, mass file renaming is a fairly common task, especially when you move Ansible inventories or other artifacts across environments.
Let's assume there is some host naming convention that allows you to make an educated guess with a glance at the host's name. For example, something like rhl-stg-itdweb01.mydomain.com gives you a pretty good idea of the OS, environment, department, and primary function.
To move your scripts from staging to the production system, you should update text and filenames to keep the Ansible inventory coherent.
Typically, I start with the mass file rename. The example below gives you an idea of producing a new name from the old one.
Let's expand this cryptic charm to a human-readable block of code below.
The next step is to update the content of the files and substitute all staging references with the production ones.
There are a few notes regarding sed processing instructions:
- The argument "-i" instructs sed to make in-place changes. Without this argument, the sed prints out results to the standard output as in the first command.
- You may notice a subtle difference in the search and replacement instructions for files and content. The latter one ends up with the "/g" which stands for"global." It may have no much sense for file names, but should always be used to replace all entries in the file body.
- Most of the time, we use "/" as an argument separator, but any non-space character will work just fine. My second choice is '#', and commands "s/abs/cde/g" and "s#abs#cde#g" are identical.