Linux sed Command
Complete Linux Command Reference
The Linux sed command uses scripts to process text files.
sed can process and edit text files according to script instructions.
Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, write conversion programs, etc.
Syntax
sed [-hnV][-e<script>][-f<script文件>][文本文件]
Parameter Description:
- -e<script> or --expression=<script> Use the script specified in the option to process the input text file.
- -f<script file> or --file=<script file> Use the script file specified in the option to process the input text file.
- -h or --help Display help.
- -n or --quiet or --silent Only display the result after script processing.
- -V or --version Display version information.
Action Description:
- a : Add. Strings can follow a, and these strings will appear on a new line (the next line after the current one)~
- c : Replace. Strings can follow c, and these strings can replace the lines between n1 and n2!
- d : Delete. Since it's deletion, d is usually not followed by anything;
- i : Insert. Strings can follow i, and these strings will appear on a new line (the line before the current one);
- p : Print, i.e., print the selected data. Usually p is used together with the parameter sed -n~
- s : Replace. It can directly perform replacement! Usually the s action can be used with regular expressions! For example, 1,20s/old/new/g!
Examples
Let's first create atestfilefile with the following content:
$ cat testfile #查看testfile 中的内容 HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test Google Taobao Example Tesetfile Wiki
IntestfileAdd a line after the fourth line of the file and output the result to standard output. Enter the following command at the command line prompt:
sed -e 4a\newLine testfile
UsingsedAfter the command, the output 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 Example Tesetfile Wiki
Adding/Deleting by Line
willtestfileList the content and print line numbers. Also, please delete lines 2~5!
$ nl testfile | sed '2,5d'
1 HELLO LINUX!
6 Taobao
7 Example
8 Tesetfile
9 Wiki
sed's action is2,5d, thatdmeans delete. Because lines 2-5 are deleted, the displayed data no longer has lines 2-5. In addition, originally you should use sed -e, but it's also fine without -e. Also, note that the action following sed must be enclosed in'...'two single quotes!
To delete only line 2:
$ nl testfile | sed '2d'
1 HELLO LINUX!
3 This is a linux testfile!
4 Linux test
5 Google
6 Taobao
7 Example
8 Tesetfile
9 Wiki
To delete lines 3 through the last line:
$ nl testfile | sed '3,$d'
1 HELLO LINUX!
2 Linux is a free unix-type opterating system.
After the second line (i.e., added as the third line), adddrink tea?the following text:
$ 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!
4 Linux test
5 Google
6 Taobao
7 Example
8 Tesetfile
9 Wiki
If you want to add it 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 Example
8 Tesetfile
9 Wiki
If you want to add more than two lines, add two lines of text after the second line, for exampleDrink tea or .....anddrink beer?
$ 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 Example
8 Tesetfile
9 Wiki
Each line must use a backslash\to mark a new line. In the above example, we can see that at the end of the first line there is\present.
Replacing and Displaying by Line
Replace the2-5 line's content withNo 2-5 number?
$ nl testfile | sed '2,5c No 2-5 number'
1 HELLO LINUX!
No 2-5 number
6 Taobao
7 Example
8 Tesetfile
9 Wiki
Through this method, we can replace an entire line of data.
List only lines 5-7 in the testfile:
$ nl testfile | sed -n '5,7p'
5 Google
6 Taobao
7 Example
With sed's line-based display function, you can select and display certain line numbers within a file.
Search and Display Data
Search testfile for lines withoothe keyword:
$ nl testfile | sed -n '/oo/p'
5 Google
7 Example
If root is found, in addition to outputting all lines, the matching line will also be output.
Search and Delete Data
Delete all lines in testfile that containoo, 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 Data and Execute Commands
Search testfile, findoothe corresponding line, execute a set of commands in the braces, with each command separated by semicolons. Here,oois replaced withkk, then output this line:
$ nl testfile | sed -n '/oo/{s/oo/kk/;p;q}'
5 Gkkgle
The finalqis to exit.
Search and Replace Data
In addition to whole-line processing mode, sed can also search and replace partial data on a line-by-line basis.
sed's search and replace is similar tovicommand, and the syntax format is as follows:
sed 's/要被取代的字串/新的字串/g'Replace the first occurrence of oo in each line of the testfile with the string kk, then output the file content to standard output:
sed -e 's/oo/kk/' testfile
gThe g flag indicates global search and replace, so that sed replaces all matching strings in the file. The modified content goes to standard output and does not modify the original file:
sed -e 's/oo/kk/g' testfile
Optionimakes sed modify the file:
sed -i 's/oo/kk/g' testfile
Batch process files in the current directory that start withtest:
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 .....(以下省略).....
This machine's IP is 192.168.1.100.
Delete 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, delete the remaining part, i.e.:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0。
Delete the part after the IP:
$ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g' 192.168.1.100
Multiple Point Editing
A single sed command to delete data from line 3 to the end of testfile and replace HELLO with EXAMPLE:
$ nl testfile | sed -e '3,$d' -e 's/HELLO/EXAMPLE/'
1 EXAMPLE LINUX!
2 Linux is a free unix-type opterating system.
-e indicates multiple point editing. The first edit command deletes the data from line 3 to the end of testfile, and the second command searches for HELLO and replaces it with EXAMPLE.
Directly Modifying File Contents (Dangerous Action)
sed can directly modify file contents without using pipe commands or data stream redirection! However, since this action directly modifies the original file, please don't casually test it with system configuration! We'll still use the file regular_express.txt to test it out!
The content of the regular_express.txt file is as follows:
$ cat regular_express.txt example. google. taobao. facebook. zhihu- weibo-
Use sed to replace the ending . of each line in regular_express.txt with !
$ sed -i 's/\.$/\!/g' regular_express.txt $ cat regular_express.txt example! google! taobao! facebook! zhihu- weibo-:q:q
Use sed to directly add to the last line of regular_express.txt:# This is a test:
$ sed -i '$a # This is a test' regular_express.txt $ cat regular_express.txt example! google! taobao! facebook! zhihu- weibo- # This is a test
Since $ represents the last line, and the a action is to add, the file has added at the end# This is a test!
sed's-ioption can directly modify file contents, and this feature is very helpful! For example, if you have a file with 1 million lines and you need to add some text at line 100, using vim might drive you crazy! Because the file is too large! What to do then? Just use sed! With sed's direct modification/replacement feature, you don't even need to use vim to edit!
Other Extensions