how to change in bulk using sed
sed
is a powerful terminal tool to perform basic text
transformations on an input stream (file or input from a pipeline).
You run the command in “dry-run” to preview the changes the command will perform. But it’s quite difficult to follow the changes if you are running the command recursively in a folder.
In this post, we will what we can use to run sed
without worrying and with the ability to rollback
in case something went wrong.
Using terminal basic commands
There are other powerful tools that can be used along with sed
to perform the operation in
“dry-run”.
First, we need to fetch all file names that needs to be changed. We will be using find
:
Explanation about the arguments:
/path/to/folder
: is the folder path in which I want to perform the changes-type f
: only target files, not folders-not -path ".git/*"
: exclude the git folder-name "*.go"
: only target golang files
We want to change the text text-to-change
into new-text
. We will use sed
to perform the change
without overwriting the files:
As we want to check the difference before and after the change, we will use the diff
command:
Iterate to all the targeted files:
However, with the previous command, everything will be displayed in your console output, which can
be enormous if you have lots of files that need to be changed. So we will use less
to “buffer” the
output so that we can follow the change at our own rhythm:
As you may have notices, by doing this, it’s not really helping as it will open a new buffer to read the difference for each file, which is not really user-friendly.
So, a way is to concatenate all the diff into a temporary file, then use less
to view the changes
in a single buffer. We will use mktemp
to create this temporary file.
You can also use colorDiff
to pretty print your output.
Putting all together:
If you are now sure to change the content, use sed
with the -i
flag:
Using GIT
Using git is more straightforward: