Linux之sed练习掌握
1。操作文本 的内容。 cat sedtest.txt
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line.
2.sed数据添加。a:append
a-追加:向匹配行后面插入内容。i-插入:向匹配行前插入内容。这里之所以要同时介绍这 2 个脚本命令,因为它们的基本格式完全相同,如下所示:
[address]a(或 i)\新文本内容
2.1 a和i命令插入单行数据实例:
[root@ecs-76840553 sed]# cat sedtest.txt | sed '3i\insert' This is the header line. This is the first data line. insert This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '3a\insert' This is the header line. This is the first data line. This is the second data line. insert This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
2.2 将多行数据添加到数据流中,只需对要插入或附加的文本中的每一行末尾(除最后一行)添加反斜线\即可,例如:
[root@ecs-76840553 sed]# sed '3i\ > insert\ > insert2\ > insert3 > ' sedtest.txt This is the header line. This is the first data line. insert insert2 insert3 This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.sed数据删除
d-删除:删除匹配的内容
如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。但使用该命令时要特别小心,如果你忘记指定具体行的话,文件中的所有内容都会被删除
命令格式:
[address]d
3.1 删除单行,指定地址
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3d' sedtest.txt This is the header line. This is the first data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.2 删除多行,指定区间。
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '2,3d' sedtest.txt This is the header line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.3 匹配文本删除
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/d' sedtest.txt This is the header line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
使用两个文本模式来删除某个区间内的行,但这么做时要小心,你指定的第一个模式会“打开”行删除功能,第二个模式会“关闭”行删除功能,因此,sed 会删除两个指定行之间的所有行(包括指定的行)
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/,/fourth/d' sedtest.txt This is the header line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
或者通过特殊的文件结尾字符,比如删除 data6.txt 文件内容中第 3 行开始的所有的内容:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3,$d' sedtest.txt This is the header line. This is the first data line. [root@ecs-76840553 sed]#
4.sed数据更改
整行数据更改:
c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。该命令的基本格式为:
[address]c\用于替换的新文本
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3c\change line' sedtest.txt This is the header line. This is the first data line. change line This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/c\change line' sedtest.txt This is the header line. change line This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
单个字符替换:
y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:
[address]y/inchars/outchars/
转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。举个简单例子:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt |sed 'y/line/ABCD/' ThBs Bs thD hDadDr ABCD. ThBs Bs thD fBrst data ABCD. ThBs Bs thD sDcoCd data ABCD. ThBs Bs thD thBrd data ABCD. ThBs Bs thD fourth data ABCD. ThBs Bs thD fBfth data ABCD. ThBs Bs thD sBxth data ABCD. ThBs Bs thD Aast ABCD. [root@ecs-76840553 sed]#
可以看到,inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相同位置的那个字符。 转换命令是一个全局命令,也就是说,它会将文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置,我们无法限定只转换在特定地方出现的字符。
字符串替换:
s-替换:替换掉匹配的内容,配合g使用,g代表全局
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed 's/line/change line/g' This is the header change line. This is the first data change line. This is the second data change line. This is the third data change line. This is the fourth data change line. This is the fifth data change line. This is the sixth data change line. This is the last change line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '2s/line/change line/g' This is the header line. This is the first data change line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '/first/s/line/change line/g' This is the header line. This is the first data change line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
5.sed数据打印
p 命令表示搜索匹配文本模式的行,并输出该行的内容,此命令的基本格式为:
[address]p
p匹配单行打印:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed -n '3p' This is the second data line. [root@ecs-76840553 sed]#
可以看到,用 -n 选项和 p 命令配合使用,我们可以禁止输出其他行,只打印包含匹配文本模式的行。 如果需要在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行,如下所示:
[root@ecs-76840553 sed]# cat sedtest.txt | sed -n '3{ p s/line/change line/ p}' This is the second data line. This is the second data change line.
sed 命令会查找包含数字 3 的行,然后执行两条命令。首先,脚本用 p 命令来打印出原始行;然后它用 s 命令替换文本,并用 p 标记打印出替换结果。输出同时显示了原来的行文本和新的行文本。
6. sed w写入文件脚本命令:
w 命令用来将文本中指定行的内容写入文件中,此命令的基本格式如下:
[address]w filename
注意:使用此命令时会覆盖掉原有的内容。
这里的 filename 表示文件名,可以使用相对路径或绝对路径,但不管是哪种,运行 sed 命令的用户都必须有文件的写权限。 下面的例子是将数据流中的1到3打印到一个文本文件中:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed -n '1,3w 1.txt' [root@ecs-76840553 sed]# cat 1.txt This is the header line. This is the first data line. This is the second data line. [root@ecs-76840553 sed]#
7.sed r 读取文件脚本命令:
r 命令用于将一个独立文件的数据插入到当前数据流的指定位置,该命令的基本格式为:
[address]r filename
sed 命令会将 filename 文件中的内容插入到 address 指定行的后面,比如说:
[root@ecs-76840553 sed]# cat sedtest.txt | sed '3r number.txt' This is the header line. This is the first data line. This is the second data line. 11 2n 333 This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
如果你想将指定文件中的数据插入到数据流的末尾,可以使用 $ 地址符,例如:
[root@ecs-76840553 sed]# cat number.txt 11 2n 333 [root@ecs-76840553 sed]# cat sedtest.txt | sed '$r number.txt' This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. 11 2n 333 [root@ecs-76840553 sed]#
8.sed q 退出脚本命令:
q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理。 比如:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '2q' This is the header line. This is the first data line. [root@ecs-76840553 sed]#
可以看到,sed 命令在打印输出第 2 行之后,就停止了,是 q 命令造成的,再比如:
[root@ecs-76840553 sed]# cat sedtest.txt | sed 's/line/change line/g;q;' This is the header change line. [root@ecs-76840553 sed]#
使用 q 命令之后,sed 命令会在匹配到line时,将其替换成 change line,然后直接退出。
9.正则表达式允许创建高级文本模式匹配表达式来匹配各种数据.
这些表达式结合了一系列通配符、特殊字符以及固定文本字符来生成能够匹配几乎任何形式文本的简练模式。例如:
[root@localhost ~]# cat test.txt <html> <title>First Wed</title> <body> h1Helloh1 h2Helloh2 h3Helloh3 </body> </html> #使用正则表示式给所有第一个的h1、h2、h3添加<>,给第二个h1、h2、h3添加</> [root@localhost ~]# cat sed.sh /h[0-9]/{ s//\<&\>/1 s//\<\/&\>/2 } [root@localhost ~]# sed -f sed.sh test.txt <html> <title>First Wed</title> <body> <h1>Hello</h1> <h2>Hello</h2> <h3>Hello</h3> </body> </html>
解释: 在上述命令中,"h[0-9]" 来匹配 h1、h2、h3,匹配到之后,把h前面的空白替换成<>,然后&表示将正则表达式匹配的内容(h1、h2、h3)替换到此处,最终的形式为<h1>、<h2>、<h3>,第二个替换命令原理也是这样。
10.sed 多行命令
在学习 sed 命令的基础功能时,你可能注意到了一个局限,即所有的 sed 命令都只是针对单行数据执行操作,在 sed 命令读取缓冲区中的文本数据时,它会基于换行符的位置,将数据分成行,sed 会根据定义好的脚本命令一次处理一行数据。 但是,有时我们需要对跨多行的数据执行特定操作。比如说,在文本中查找一串字符串"http://c.biancheng.net",它很有可能出现在两行中,每行各包含其中一部分。这时,如果用普通的 sed 编辑器命令来处理文本,就不可能发现这种被分开的情况。 幸运的是,sed 命令的设计人员已经考虑到了这种情况,并设计了对应的解决方案。sed 包含了三个可用来处理多行文本的特殊命令,分别是:
1、Next 命令(N):将数据流中的下一行加进来创建一个多行组来处理。
2、Delete(D):删除多行组中的一行。
3、Print(P):打印多行组中的一行。
注意,以上命令的缩写,都为大写。
10.1多行命令
N 命令会将下一行文本内容添加到缓冲区已有数据之后(之间用换行符分隔),从而使前后两个文本行同时位于缓冲区中,sed 命令会将这两行数据当成一行来处理。 下面这个例子演示的 N 命令的功能:
[root@ecs-76840553 sed]# cat sedtest.txt |sed 'N;s/\n/ /g' This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt |sed '/head/N;s/\n/ /g' This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#