The use and detailed interpretation of sed editor

The use and detailed interpretation of sed editor

One, sed editor

Sed is a stream editor, the stream editor will edit the data stream based on a set of rules provided in advance before the editor processes the data.
The sed editor can process the data in the data stream according to commands, which are either input from the command line or stored in a command text file

1. The workflow of sed

(1), read

sed reads a line of content from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space).

(2) Implementation

By default, all sed commands are executed sequentially in the pattern space. Unless the address of the line is specified, the sed command will be executed sequentially on all lines.

(3), display

Send the modified content to the output stream. After sending the data, the pattern space will be cleared. Before all file contents are processed, the above process will be repeated until all contents are processed.

Before all file contents are processed, the above process will be repeated until all contents are processed.
Note: By default, all sed commands are executed in the pattern space, so the input file will not change in any way, unless redirection is used to store the output.

2. Command explanation

命令格式:
sed -e '操作' 文件1 文件2 ...
sed -n -e '操作' 文件1 文件2 ...
sed -f 脚本文件 文件1 文件2 ...
sed -i -e '操作' 文件1 文件2 .

sed -e 'n{
操作1
操作2
...
}' 文件1 文件2 ...

Common options:

-e 或--expression=:表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令使用
-f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
-h 或--help:显示帮助。
-n、--quiet 或 silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。
-i:直接修改目标文本文件。

Common operations:

s:替换,替换指定字符。
d:删除,删除选定的行。
a:增加,在当前行下面增加一行指定内容。
i:插入,在选定行上面插入一行指定内容。
c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
=:打印行号。
l(小写L):打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

Print content:

sed -n -e 'p' test1.txt 

#不指定行 默认打印所有内容

[root@localhost ~]#sed -n -e 'p' test1.txt 
one
two
three
four
five
six
sed -n -e '='  test1.txt 

#打印文件内的行号

[root@localhost ~]#sed -n -e '=' test1.txt 
1
2
3
4
5
6
sed -n -e 'l'  test1.txt 

#打印文本和ASCII码

[root@localhost ~]#sed -n -e 'l' test1.txt 
one$
two$
three$
four$
five$
six$
sed -n -e '=;p'test1.txt 或
sed -n -e '=' -e 'p' test1.txt

sed -n '
> =
> p
> ' test1.txt
#打印文件中的文本和行号

[root@localhost ~]#sed -n -e '=;p' test1.txt 
1
one
2
two
3
three
4
four
5
five
6
six

(1) Use address:

The sed editor has 2 addressing modes:
1. Express the line interval in number form
2. Use text mode to filter out lines

sed -n '1p' test1.txt    #打印第一行

[root@localhost ~]#sed -n '1p' test1.txt
one
sed -n '$p' test1       #打印最后一行

[root@localhost ~]#sed -n '$p' test1.txt
six
sed -n '1,3p' test1.txt   #打印1到3行

[root@localhost ~]#sed -n '1,3p' test1.txt 
one
two
three
sed -n '3,$p' test1.txt  #打印3行到最后一行

[root@localhost ~]#sed -n '3,$p' test1.txt 
three
four
five
six
sed -n '1,+3p' test1.txt   #打印1之后的连续3行,即1-4行

[root@localhost ~]#sed -n '1,+3p' test1.txt
one
two
three
four

s

ed '5q' test1.txt   #打印前5行信息后退出,q表示退出

[root@localhost ~]#sed '5q' test1.txt 
one
two
three
four
five
sed -n 'p;n' test1.txt   #打印奇数行(先从第一行打印,然后隔一行打印一次);n表示移动到下一行

[root@localhost ~]#sed -n 'p;n' test1.txt
one
three
five
sed -n 'n;p' test1.txt   		#打印偶数行 (先跳到第二行,然后打印,然后在跳过)

[root@localhost ~]#sed -n 'n;p' test1.txt
two
four
six
sed -n '2,${n;p}' test1.txt   #先跳到第二行,然后跳到下一行开始打印,从第三行开始打印奇数行

[root@localhost ~]#sed -n '2,${n;p}' test1.txt 
three
five
sed -n '/user/p' /etc/passwd    #打印/etc/passwd 文件内有user的行

[root@localhost ~]#sed -n '/user/p' /etc/passwd
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sed -n '/^a/p' /etc/passwd     #打印以a开头的行

[root@localhost ~]#sed -n '/^a/p' /etc/passwd    
adm:x:3:4:adm:/var/adm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sed -n '/bash$/p' /etc/passwd  #打印以bash结尾的行

Insert picture description here

sed -n '/ftp\|root/p' /etc/passwd  # 打印包含ftp或者root的行 

Insert picture description here

sed -n '2,/nobody/p' /etc/passwd   #从第二行开始打印出第一个包含nobody的行

Insert picture description here

Insert picture description here

sed -n '/nobody/=’ /etc/passwd     #打印包含nobody的行号

Insert picture description here

sed -nr '/ro{1,}t/p' /etc/passwd	#-r表示支持正则表达式
#匹配已ro开头,已t结尾,中间至少包含一个o的行

Insert picture description here

(2), delete the line

sed 'd' test.txt #全删

[root@localhost ~]#sed -i 'd' test.txt
[root@localhost ~]#cat test.txt 

sed '3d' test.txt #Delete the third line

[root@localhost ~]#sed '3d' test.txt
one 
two
four
five
six

seven #You can see that the third line is gone

sed '2,4d' test.txt #Delete 2 to 4 lines

[root@localhost ~]#sed '2,4d' test.txt 
one 
five
six
seven   #可以看到234行都没有了
sed '$d' test.txt   #删除最后一行

[root@localhost ~]#sed '$d' test.txt
one 
two
three
four
five
six   #第七行seven没有了
sed '/^$/d' .test.txt    #删除空行

Insert picture description here

sed '/bash$/d' /etc/passwd    #删除已bash结尾的行

Insert picture description here

sed '/nologin$/!d' /etc/passwd		#“!”表示取反操作  不删除nologin结尾的行,也就是除了nologin结尾的行都删除

Insert picture description here

sed '/2/,/3/d' test.txt		#从第一个位置打开行删除功能,到第二个位置关闭行删除功能
sed '/1/,/3/d' test.txt

Insert picture description here

(3) Replace

行范围 s/旧字符串/新字符串/替换标记

4 types of replacement tags:

数字:表明新字符串将替换第几处匹配的地方
g:表明新字符串将会替换所有匹配的地方
p:打印与替换命令匹配的行,与-n一起使用
w 文件:将替换的结果写到文件中
sed -n 's/root/admin/p' /etc/passwd   #把root替换成admin 默认第一个root

Insert picture description here
Insert picture description here

sed -n 's/root/admin/2p' /etc/passwd  #把每一行的第二个root替换成admin

Insert picture description here

sed -n 's/root/admin/gp' /etc/passwd   #把root全部替换成admin

Insert picture description here

sed 's/root//g' /etc/passwd            #把所有的root换成空,就是删除的意思

Insert picture description here

sed '1,20 s/^/#/' /etc/passwd          #在1-20行的行首添加#号

Insert picture description here

sed '/^root/ s/$/#/' /etc/passwd       #找到root开头的行,在结尾加上#

Insert picture description here

sed '1,20w out.txt' /etc/passwd         #把1-20行输出保存到out.txt文件内
sed '1,20 s/^/#/w out.txt' /etc/passwd  #把1-20行的开头加上#号 输出到out.txt文件内

```handlebars
sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd  #
sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd		#使用“!”作为字符串分隔符
把bash替换成csh

Insert picture description here

sed '/three/c ABC' test.txt#搜索到包含45的行,整行替换成ABC

Insert picture description here

sed '/3/ y/3/A/' test.txt    #搜索到3的行,把3替换成相同字符串长度的A

Insert picture description here

sed '1,3a ABC' testfile2       # 1-3行下面分别插入ABC

Insert picture description here

sed '1i ABC' test.txt       #1行上面加ABC

Insert picture description here

sed '5r /etc/resolv.conf' test.txt      # /etc/resolv文件导入test第五行

Insert picture description here

sed '/root/{H;d};$G' /etc/passwd	#将包含root的行剪切到末尾,H表示复制到剪切板,G表示粘贴到指定行后

Insert picture description here

Insert picture description here

sed '1,5H;15G' /etc/passwd    #把1-5行复制到15行后面

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/111801666