-n:使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e:直接在指令列模式上进行 sed 的动作编辑;
-f:直接将 sed 的动作写在一个档案内,-f filename 则可以执行 filename 内的sed 动作;
-r:sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i:直接修改读取的档案内容,而不是由萤幕输出
a:新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c:取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d:删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i:插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p:列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s:替换,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
命令 | 功能 |
---|---|
n | 读取下一行数据到模式空间 |
N | 将下一行追加到模式空间后 |
d | 删除模式空间中的所有行,并读入下一行 |
D | 删除模式空间的第一行,不读入下一行 |
h | 将模式空间中的数据复制到保留空间 |
H | 将模式空间中的数据追加到保留空间 |
g | 将保留空间中的数据复制到模式空间 |
G | 将保留空间中的数据追加到模式空间 |
x | 将模式空间和保留空间中的数据对调 |
b | label 跳转到标签label,如果没有label则跳转到指令的结尾 |
d:删除
[root@localhost ~]# cat anaconda-ks.cfg
#version=RHEL8
# Use graphical install
graphical # 删除此行
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
[root@localhost ~]# sed '/^graphical/d' anaconda-ks.cfg # 精准匹配到删除
#version=RHEL8
# Use graphical install #原本在下面的删除掉了
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
%packages
@^minimal-environment
kexec-tools
[root@localhost ~]# cat anaconda-ks.cfg -n #查看行号
1 #version=RHEL8
2 # Use graphical install
3 graphical # 第三行
4
5 repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
6
7 %packages
8 @^minimal-environment
[root@localhost ~]# sed '3d' anaconda-ks.cfg #利用行号删除
#version=RHEL8
# Use graphical install
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
%packages
@^minimal-environment
kexec-tools
替换元字符
[root@localhost ~]# echo '/usr/local/src' | sed 's/\/usr\/local\/src/\/user\/local\/src/' # 路径的斜杠每一个都要加转义符很麻烦
/user/local/src
[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src#' # 可以用#代替
/user/local/src
[root@localhost ~]# echo '/usr/local/src' | sed 's!/usr/local/src!/user/local/src!' #也可以用!代替
/user/local/src
. 表示一个正真的制表符,而制表符在屏幕上是看不到的,如果输入一行文件如下所示:
[root@localhost ~]# vi abc
[root@localhost ~]# cat abc
Column1 Column2 Column3 Column4
[root@localhost ~]# sed 's/\t/>/2' abc #\t是制表符,把第二个换成>
Column1 Column2>Column3 Column4
[root@localhost ~]# sed 's/\t/\ # 第二个制表符换行
> /2' abc
> Column1 Column2
> Column3 Column4
[root@localhost ~]# sed 's/\t/\n/2' abc #效果同上
Column1 Column2
Column3 Column4
[root@localhost ~]# echo '.Ah "Major Heading"' > abc
[root@localhost ~]# cat abc
.Ah "Major Heading"
[root@localhost ~]# sed '/^\.Ah/{ #匹配.A和开头的
> s/\.Ah */\ #替换.Ah开头的然后换行
> \ #在换行
> @A HEAD = / # 换成@A HEAD =
> s/"//g # 把引号替换成空的
> s/$/\ #把这个$替换然后换行
> /
> } ' abc # 更上文件文件名
@A HEAD = Major Heading #效果
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g};s/$/\n/' abc
@A HEAD = Major Heading
[root@localhost ~]# vi abc
[root@localhost ~]# cat abc
.Ah "Major Heading"
ORA Associates, Inc.
[root@localhost ~]# sed "s/ORA/O' Reilly \&/" abc #匹配到ORA替换成O' Reilly &,&有特殊意义,所以要加转义符
.Ah "Major Heading"
O' Reilly & Associates, Inc.
[root@localhost ~]# cat abc
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
[root@localhost ~]# sed 's/UNIX/\\s-2&\\s0/g' abc #匹配到的UNIX的前面加上\s-2 在前面加就是在&前面写,在后面就在&后面写
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating Systen.
[root@localhost ~]# vi abc
[root@localhost ~]# cat abc
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
[root@localhost ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g'abc # *表示前面的0-9可以出现任意次,点加上转义符,就是.本身,括号把&扩起来就是把整行内容扩起来
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
(See Section 1.4)
(See Section 12.9)
[root@localhost ~]# sed -r 's/(See Section) ([1-9][0-9]*\.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc # 用括号把See Section括起来,把数字也括起来,后面\1先打印出来,然后\2代表的是数字然后在前面后面替换上内容
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section\fb1.4\fp
See Section\fb12.9\fp
[root@localhost ~]# cat abc
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed -r 's/(.*):(.*)/\2:\1/g' abc # 第一个.*匹配到的是:前面的内容\1表示,第2个.*匹配到的是:后面的内容\2表示。把\2的内容放到前面,\1的放到后面,-r是正则表达式
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
second:first
two:one
删除d
[root@localhost ~]# cat abc
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/d' abc # 匹配到.Ah开头的删除
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
追加(a)插入(i)修改(c)
a追加
[root@localhost ~]# sed '1aass' abc # 在第1行后面加入ass
.Ah "Major Heading"
ass
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1 a ass' abc # 在内容前面加空格,下面的显示也没有
.Ah "Major Heading"
ass
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1a " ass"' abc # 把加入的内容引起来,就会有了
.Ah "Major Heading"
" ass"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1a \ ass' abc # 用转义符也可以
.Ah "Major Heading"
ass
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^See/a 123' abc #匹配的See哪两行然后追加
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
123
See Section 12.9
123
first:second
one:two
[root@localhost ~]# cat abc
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '/See.*[1-9][0-9]\.[0-9]/aabc' abc # 精确匹配到这两行加入内容
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
abc
See Section 13.5
abc
first:second
one:two
i插入
[root@localhost ~]# sed '1ihehe' abc # 在第一行插入内容,插入的内容就变成第一行了
hehe
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/i abc' abc #匹配到.Ah的这行插入内容
abc
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '/See.*[1-9][0-9]\.[0-9]/i abc' abc #也可以精确匹配到这两行插入
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
abc
See Section 12.9
abc
See Section 13.5
first:second
one:two
c更改
[root@localhost ~]# cat abc
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '1c hehe' abc #第一行被修改为hehe
hehe
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '/aAh/c \xixi' abc #匹配到aAh的这行更改为xixi
.Ah "Major Heading"
xixi
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/iabc\hehe' abc
abc
hehe
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
转换
[root@localhost ~]# cat abc
.Ah "Major Heading"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two
[root@localhost ~]# sed '1y/ajo/123/' abc #把第一行里面的ajo转换成123
.Ah "M123r He1ding"
aAh fvgerbevdve
ORA Associates, Inc.
on the UNIX Operating Systen.
See Section 1.4
See Section 12.9
See Section 13.5
first:second
one:two[
打印
[root@localhost ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' 2 # 匹配到.Ah的行,先p打印,然后把"换成空,匹配.Ah换成空并打印
.Ah "Comment"
Comment
.Ah "Substitution"
Substitution
.Ah "Delete"
Delete
.Ah "Append, Insert and Change"
Append, Insert and Change
.Ah "List"
List
打印行号
[root@localhost ~]# sed '/Comment/{=;p}' 2
1
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# sed -n '/Comment/{=;p}' 2
1
.Ah "Comment"
[root@localhost ~]#
下一步
[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# sed -n '/Comment/n;p' 2 #没有打印Comment,从他的下一个开始打印的
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# sed -r '/Comment/{n;s/\.Ah (.*)/\.Ah (\1)/g}' 2 # Comment下一个以.Ah开头的用括号扩起来
.Ah "Comment"
.Ah ("Substitution")
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Comment"
.Ah "Append, Insert and Change"
.Ah "Comment"
.Ah "List"
[root@localhost ~]# sed -r '/Comment/{n;s/\.Ah (.*)/\.Ah (\1)/g}' 2
.Ah "Comment"
.Ah ("Substitution")
.Ah "Delete"
.Ah "Comment"
.Ah ("Append, Insert and Change")
.Ah "Comment"
.Ah ("List")
[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Comment"
.Ah "Append, Insert and Change"
.Ah "Comment"
.Ah "List"
[root@localhost ~]# sed '/Comment/{n;/^$/d}' 2 # 匹配到Comment下一行,如果是空行就删掉
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Comment"
.Ah "Append, Insert and Change"
.Ah "Comment"
.Ah "List"
N:追加下一行
[root@apache ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
[root@apache ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide\n/;p}' abc
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
[root@apache ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
avai lable on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided inc luding the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.
[root@apache ~]# cat test
s/Owner and Operator Guide/ Installation Guide/
/Owner/ {
N
s/ *\n/ /
s/Owner and Operator Guide */Installation Guide\
/
}
[root@apache ~]# sed -f test abc
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
avai lable on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided inc luding the Installation Guide
and the User Guide.
The Installation Guide is shipped with your system.
D:删除模式空间第一行
[root@apache ~]# cat abc
hello world
hello world
hello world
hello world
hello world
[root@apache ~]# sed '/^$/{N;/^\n$/d}' abc
hello world
hello world
hello world
hello world
hello world
[root@apache ~]# sed '/^$/{N;/^\n$/D}' abc
hello world
hello world
hello world
hello world
hello world
P:多行打印
[root@apache ~]# cat abc
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operat ing System.
[root@apache ~]# cat test
/UNIX$/ {
N
/\nSystem/ {
s// Operating &/
P
D
}
}
[root@apache ~]# sed -f test aaa
Here are examples of the UNIX Operating
System. Where UNIX Operating
System appears,it should be the UNIX
Operating System.
命令 | 缩写 | 功能 |
---|---|---|
Hold | h或H | 将模式空间的内容复制或追加到保持空间 |
Get | g或G | 将保持空间的内容复制或追加到模式空间 |
Exchange | x | 交换保持空间和模式空间的内容 |
[root@apache ~]# cat abc
1
2
11
22
111
222
[root@apache ~]# sed -n '/1/{h;d};/2/{G;p}' abc
2
1
22
11
222
111
y
[root@apache ~]# cat abc
find the Match statement
Consult the Get statement.
Using the Read statement to retrieve data
[root@apache ~]# sed -rn '/the .* statement/{h;s/.*the(.*)statement.*/\1/;y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/;G;s/(.*)\n(.*the).*(statement.*)/\2\1\3/;p}' abc
find the MATCH statement
Consult the GET statement.
Using the READ statement to retrieve data
awk
[root@apache ~]# cat aaa
this line of data is ignored
[root@apache ~]# awk '{print "Hello, world"}' aaa
Hello, world
[root@apache ~]# echo '' >> aaa
[root@apache ~]# echo '' >> aaa
[root@apache ~]# awk '{print "Hello, world"}' aaa
Hello, world
Hello, world
Hello, world
[root@apache ~]# awk '/^$/ {print "Hello, world"}' aaa
Hello, world
Hello, world
[root@apache ~]# echo 'a b c d'|awk 'BEGIN{one = 1; two = 2 }{print $(one + two)}'
c