One, sed overview
Sed is the abbreviation of Stream Editor, used to process files.
Working principle :
sed reads the file line by line, reads the file content and processes it as required, and outputs the processed results to the screen
- First sed reads the content of a line in the file and saves it in aTemporary buffer(Also called pattern space)
- thenAccording to demandProcess the line in the temporary buffer, and put the line after completionSend to screen
to sum up:
- Since sed stores each line in a temporary buffer, the copy is edited, soWill not directly modify the original file
- Sed is mainly used to automatically edit one or more files; to simplify repeated operations on files, filter and convert files
2. Introduction to the use of sed
There are two common syntax formats for sed, one is called command line mode, and the other is called script mode.
(1) Command line mode
(1) Grammar format
sed [options] '处理动作' 文件名
- Common options
Options | Description | Remarks |
---|---|---|
-e | Make multiple (multiple) edits | |
-n | Cancel the default output | Do not automatically print mode space |
-r | Use extensionRegular expression | |
-i | Edit in place (modify source file) | |
-f | Specify the file name of the sed script |
- Common processing actions
The ugly words come first : all of the followingactionTo be in single quotes in
action | Description | Remarks |
---|---|---|
‘p’ | ||
‘i’ | In the specified lineprior toInsert content | Similar to uppercase O in vim |
‘a’ | In the specified lineafter thatInsert content | Similar to the lowercase o in vim |
‘c’ | Replace all contents of the specified line | |
‘d’ | Delete specified line |
(2) Examples
① PerformAdd, delete, modify, checkoperating
Syntax: sed option 'Positioning + Command’ Documents to be processed
1) Print file content
[root@server ~]# sed '' a.txt 对文件什么都不做
[root@server ~]# sed -n 'p' a.txt 打印每一行,并取消默认输出
[root@server ~]# sed -n '1p' a.txt 打印第1行
[root@server ~]# sed -n '2p' a.txt 打印第2行
[root@server ~]# sed -n '1,5p' a.txt 打印1到5行
[root@server ~]# sed -n '$p' a.txt 打印最后1行
2) Increase the content of the file
i insert
a above and insert below
[root@server ~]# sed '$a99999' a.txt 文件最后一行下面增加内容
[root@server ~]# sed 'a99999' a.txt 文件每行下面增加内容
[root@server ~]# sed '5a99999' a.txt 文件第5行下面增加内容
[root@server ~]# sed '$i99999' a.txt 文件最后一行上一行增加内容
[root@server ~]# sed 'i99999' a.txt 文件每行上一行增加内容
[root@server ~]# sed '6i99999' a.txt 文件第6行上一行增加内容
[root@server ~]# sed '/^uucp/ihello' 以uucp开头行的上一行插入内容
3) Modify the content of the file
c to replace the specifiedWhole linecontent
[root@server ~]# sed '5chello world' a.txt 替换文件第5行内容
[root@server ~]# sed 'chello world' a.txt 替换文件所有内容
[root@server ~]# sed '1,5chello world' a.txt 替换文件1到5号内容为hello world
[root@server ~]# sed '/^user01/c888888' a.txt 替换以user01开头的行
4) Delete file content
d delete
[root@server ~]# sed '1d' a.txt 删除文件第1行
[root@server ~]# sed '1,5d' a.txt 删除文件1到5行
[root@server ~]# sed '$d' a.txt 删除文件最后一行
②Search and replace files
Syntax: sed option 's/searched content/replaced content/action' Documents are processed,
whereinsIndicates search; slash/Indicates the separator, which can be defined by yourself; the action is generally printingpAnd global replacementg
[root@server ~]# sed -n 's/root/ROOT/p' 1.txt
[root@server ~]# sed -n 's/root/ROOT/gp' 1.txt
[root@server ~]# sed -n 's/^#//gp' 1.txt
[root@server ~]# sed -n 's@/sbin/nologin@itcast@gp' a.txt
[root@server ~]# sed -n 's/\/sbin\/nologin/itcast/gp' a.txt
[root@server ~]# sed -n '10s#/sbin/nologin#itcast#p' a.txt
uucp:x:10:14:uucp:/var/spool/uucp:itcast
[root@server ~]# sed -n 's@/sbin/nologin@itcastheima@p' 2.txt
注意:搜索替换中的分隔符可以自己指定
[root@server ~]# sed -n '1,5s/^/#/p' a.txt 注释掉文件的1-5行内容
#root:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#daemon:x:2:2:daemon:/sbin:/sbin/nologin
#adm:x:3:4:adm:/var/adm:/sbin/nologin
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
To be continued. . .