sed
Replaces a string. This is a very useful command and is used in many places. It not only replaces the string, but also adds or removes new lines.
NAME
sed -- stream editor
SYNOPSIS
sed [-Ealn] command [file ...].
sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...]]
github
- The file in jupyter notebook format on github is here .
google colaboratory
- To run it in google colaboratory here sed_nb.ipynb)
environment
The author’s OS is macOS, and the options are different from those of Linux and Unix commands.
If you want to run the command in real life, you need to replace the leading ! and %%bash in the first line.
!sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G2022
!bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
Example usage
Basically, you can use
sed s/before/after/g <FILE>
to replace the string “before” in the file with “after”.
sed -ei s/before/after/g <FILE>
You can overwrite the file with the ei option, or with the i option if you are not on MAC. The final g will replace all before’s in the file. If not, only the first matching before is replaced.
%%bash
echo "Prepare the file."
echo "=== example : before ===" > temp
cat temp
sed -ie "s/before/after/g" temp
cat temp
Prepare the file.
=== example : before ===
=== example : after ===
Typical options
- e
- i