Linux sed Command
The Linux sed command is used to process text files with scripts.
sed can process and edit text files according to the instructions in a script.
Sed is primarily used for automatically editing one or multiple files, simplifying repeated operations on files, and writing conversion programs.
Syntax
sed [-hnV][-e<script>][-f<script file>][text file]
Parameter Description:
-e<script> or --expression=<script> Process the input text file with the script specified in the option.
-f<script file> or --file=<script file> Process the input text file with the script file specified in the option.
-h or --help Display help.
-n or --quiet or --silent Only display the result after the script has been processed.
-V or --version Display version information.
Action Description:
a : Append. Strings following "a" will appear on a new line (the next line).
c : Replace. Strings following "c" will replace the lines between n1 and n2.
d : Delete. Since it's deletion, "d" is usually not followed by anything.
i : Insert. Strings following "i" will appear on a new line (the previous line).
p : Print, i.e., output the selected data. Usually "p" is run with the sed -n parameter.
s : Substitute. Directly perform substitution. This action "s" can often be paired with regular expressions. For example, 1,20s/old/new/g.
Example
First, create a testfile with the following content:
$ cat testfile # View the content of testfile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Google
Taobao
tutorialpro
Tesetfile
Wiki
Add a new line after the fourth line in the testfile and output the result to standard output. Enter the following command at the command prompt:
sed -e 4a\newLine testfile
Using the sed
command, the output result is as follows:
$ sed -e 4a\newLine testfile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
newLine
Google
Taobao
tutorialpro
Tesetfile
Wiki
Adding/Deleting Lines by Line Number
List the content of testfile with line numbers and delete lines 2 through 5:
$ nl testfile | sed '2,5d'
1 HELLO LINUX!
6 Taobao
7 tutorialpro
8 Tesetfile
9 Wiki
The action for sed is 2,5d
, where d means delete. Since lines 2-5 are deleted, the displayed data does not include these lines. Additionally, although sed -e should be used, omitting -e also works. Also, note that the actions following sed should be enclosed in '...'
.
Delete only the second line:
$ nl testfile | sed '2d'
1 HELLO LINUX!
3 This is a linux testfile!
4 Linux test
5 Google
6 Taobao
7 tutorialpro
8 Tesetfile
9 Wiki
Delete from the third line to the last line:
$ nl testfile | sed '3,$d'
1 HELLO LINUX!
2 Linux is a free unix-type opterating system.
Add the string drink tea? after the second line (i.e., add it to the third line):
$ nl testfile | sed '2a drink tea'
1 HELLO LINUX!
2 Linux is a free unix-type opterating system.
drink tea
3 This is a linux testfile!
1 Linux test 2 Google 3 Taobao 4 tutorialpro 5 Tesetfile 6 Wiki
If you want to insert a line before the second line, the command is as follows:
$ nl testfile | sed '2i drink tea' 1 HELLO LINUX! drink tea 2 Linux is a free unix-type opterating system. 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 tutorialpro 8 Tesetfile 9 Wiki
If you want to add more than one line, for example, adding **Drink tea or .....** and **drink beer?** after the second line:
$ nl testfile | sed '2a Drink tea or ......\ drink beer ?'
1 HELLO LINUX! 2 Linux is a free unix-type opterating system. Drink tea or ...... drink beer ? 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 tutorialpro 8 Tesetfile 9 Wiki
Each line must be marked with a backslash `\` for a new line. In the example above, we can see that there is a `\` at the end of the first line.
### Line-based Replacement and Display
Replace the content of lines **2-5** with **No 2-5 number**:
$ nl testfile | sed '2,5c No 2-5 number' 1 HELLO LINUX! No 2-5 number 6 Taobao 7 tutorialpro 8 Tesetfile 9 Wiki
Through this method, we can replace the entire line of data.
List only lines 5-7 from the testfile:
$ nl testfile | sed -n '5,7p' 5 Google 6 Taobao 7 tutorialpro
You can use this line-based display function of sed to select and display certain line numbers from a file.
### Search and Display Data
Search testfile for lines containing the keyword `oo`:
$ nl testfile | sed -n '/oo/p' 5 Google 7 tutorialpro
If `root` is found, it will output all lines and also the matching lines.
### Search and Delete Data
Delete all lines in testfile that contain `oo`, and output the other lines:
$ nl testfile | sed '/oo/d' 1 HELLO LINUX! 2 Linux is a free unix-type opterating system. 3 This is a linux testfile! 4 Linux test 6 Taobao 8 Tesetfile 9 Wiki
### Search and Execute Commands
Search testfile, find lines containing `oo`, and execute the commands within the braces, each command separated by a semicolon. Here, replace **oo** with **kk** and then output the line:
$ nl testfile | sed -n '/oo/{s/oo/kk/;p;q}' 5 Gkkgle
The final `q` is to quit.
### Search and Replace Data
In addition to processing entire lines, sed can also perform partial data search and replace on a line-by-line basis.
The search and replace syntax of sed is similar to the `vi` command, formatted as follows:
sed 's/string to be replaced/new string/g'
sed -e 's/oo/kk/' testfile
The `g` flag indicates global search and replace, causing sed to replace all matching strings in the file. The modified content will be output to the standard output and will not modify the original file:
sed -e 's/oo/kk/g' testfile
The `i` option makes sed modify the file:
sed -i 's/oo/kk/g' testfile
Batch operation on files starting with **test** in the current directory:
sed -i 's/oo/kk/g' ./test*
Next, we use `/sbin/ifconfig` to query the IP:
$ /sbin/ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84 inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 .....(following omitted).....
The local IP is 192.168.1.100.
Remove the part before the IP:
$ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
Next, remove the subsequent part, which is: **192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0**.
Remove the part after the IP:
$ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.addr://g' | sed 's/Bcast.$//g' 192.168.1.100
### Multiple Edits
A single sed command to delete data from the third line to the end of `testfile` and replace HELLO with tutorialpro:
$ nl testfile | sed -e '3,$d' -e 's/HELLO/tutorialpro/'
1 tutorialpro LINUX!
2 Linux is a free unix-type opterating system.
-e indicates multiple edits. The first edit command deletes data from the third line to the end of `testfile`, and the second command replaces HELLO with tutorialpro.
### Directly Modifying File Content (Dangerous Action)
sed can directly modify the content of files without using pipe commands or data stream redirection! However, since this action directly modifies the original file, please do not randomly test with system configurations! Let's still use the `regular_express.txt` file for testing.
The content of `regular_express.txt` is as follows:
$ cat regular_express.txt tutorialpro. google. taobao. facebook. zhihu- weibo-
Use sed to replace the ending of each line in `regular_express.txt` if it is a period with an exclamation mark:
$ sed -i 's/.$/!/g' regular_express.txt $ cat regular_express.txt tutorialpro! google! taobao! facebook! zhihu- weibo-
Use sed to directly add **# This is a test** at the end of `regular_express.txt`:
$ sed -i '$a # This is a test' regular_express.txt $ cat regular_express.txt tutorialpro! google! taobao! facebook! zhihu- weibo-
This is a test
```
Since $
represents the last line and a
is the action to append, # This is a test is added at the end of the file!
The -i
option of sed can directly modify file content, which is very helpful! For example, if you have a 1-million-line file and you need to add some text on the 100th line, using vim might be overwhelming due to the file's size. What to do? Use sed! With sed's direct modification/replacement functionality, you don't even need to use vim to edit!