南宁网站建设网站推广,惠州住房和建设局网站,ssh框架可以做网站么,成都布马网络科技有限公司以下内容整理于《linux命令行与shell脚本编程大全【第三版】》一书。 一、简介sed编辑器
1、sed编辑器的本质
sed是stream editor的缩写#xff0c;中文意思是“流编辑器”。
sed编辑器是一个命令行编辑器#xff0c;也就是可以在命令行上完成数据的处理#xff08;替换、…以下内容整理于《linux命令行与shell脚本编程大全【第三版】》一书。 一、简介sed编辑器
1、sed编辑器的本质
sed是stream editor的缩写中文意思是“流编辑器”。
sed编辑器是一个命令行编辑器也就是可以在命令行上完成数据的处理替换、删除、插入、修改、转换等操作。
sed编辑器本质是一个Shell外部命令因此也叫sed命令通过该命令可以基于某些规则来编辑数据流。
2、sed编辑器的使用
在命令行上使用sed命令的格式如下
sed option script file
1file表示要处理哪个文本文件如果没有明确指出则默认从标准输入流获得内容。
2script表示编辑器命令也就是对file进行哪些操作。
这些编辑器命令可以直接写在命令行上也可以写在一个文件中。如果编辑器命令中有空格则必须用单引号包围起来。直接写在命令行时如果有多条命令则命令之间以分号隔开此时option要写为-e。写在一个文件中时如果有多条命令则一行写一条命令即可末尾不需要分号此时option要写成-f。
3optiton的可选项包括-e、-f和-n。
-e表示在命令行上有多条编辑器命令。-f表示编辑器命令写在一个文件中。-n表示禁止sed编辑器输出。
4sed编辑器并不会修改文本文件的内容它只会将修改后的数据发送到标准输出原来的文本文件依然保留着原始的数据。
sed编辑器一次从输入文件或者标准输入流中获取一行的数据。根据所提供的编辑器命令script匹配数据。按照编辑器命令修改数据流中的数据。将修改后的数据输出到标准输出中。
5以sed编辑器命令中的文本替换命令s为例说明上述的内容。
xjhubuntu:~/iot/tmp$ cat data1.txt #data1.txt文件内容
This is a test
xjhubuntu:~/iot/tmp$ sed s/test/big test/ data1.txt #从文件中获取要处理的数据
This is a big test
xjhubuntu:~/iot/tmp$ sed -n s/test/big test/ data1.txt #添加-n选项后没有内容输出
xjhubuntu:~/iot/tmp$
xjhubuntu:~/iot/tmp$ sed -e s/test/big test/;s/This/It/ data1.txt #在命令行输入多条编辑命令用分号隔开添加-e选项
It is a big test
xjhubuntu:~/iot/tmp$ sed s/test/big test/;s/This/It/ data1.txt #没有-e选项也可以
It is a big test
xjhubuntu:~/iot/tmp$ sed s/XJH/XJQ/ #这里没有给出要处理的内容所以默认从标准输入获取因此接下来要键入内容
XJH is very handsome #输入内容
XJQ is very handsome #显示处理后的内容
My name is XJH #如果没有输入ctrld产生EOF字符则会要求一直输入并一直输入
My name is XJQ
xjhubuntu:~/iot/tmp$ cat script.sed #编辑器命令写在一个文件中后缀.sed只是为区分shell脚本非必需
s/XJH/XJQ/
s/handsome/beautiful/
s/boy/girl/
xjhubuntu:~/iot/tmp$ cat data2.txt
XJH is very handsome
XJH is a handsome boy
xjhubuntu:~/iot/tmp$ sed -f script.sed data2.txt #编辑器命令写在一个文件中需添加-f选项
XJQ is very beautiful
XJQ is a beautiful girl
xjhubuntu:~/iot/tmp$ cat data2.txt #sed编辑器不会修改文本文件的内容,原来的文本文件依然保留着原始的数据。
XJH is very handsome
XJH is a handsome boy
xjhubuntu:~/iot/tmp$ sed script.sed data2.txt
sed: -e expression #1, char 10: unterminated s command
xjhubuntu:~/iot/tmp$ sed -nf script.sed data2.txt
xjhubuntu:~/iot/tmp$ sed -n -f script.sed data2.txt
xjhubuntu:~/iot/tmp$ 二、sed编辑器的命令
sed编辑器定义了很多类型的命令比如文本替换命令s、删除命令d、插入命令i、修改命令c、转换命令y、写入命令w等命令 这些命令可以满足不同的编辑需求。
1、文本替换命令s
上面的例子是文本替换命令s的简单情形接下来讲解其复杂一些的情形。
1可以将字符串分隔符修改为其他字符。
xjhubuntu:~/iot/tmp$ cat data1.txt
This is a test
xjhubuntu:~/iot/tmp$ sed s/test/big test/ data1.txt
This is a big test
xjhubuntu:~/iot/tmp$ sed s:test:big test: data1.txt
This is a big test
xjhubuntu:~/iot/tmp$ sed s!test!big test! data1.txt
This is a big test
xjhubuntu:~/iot/tmp$
2可以选用替换标志来控制替换的方式。格式如下
s/pattern/replacement/flags
数字表示替换第几处模式匹配的地方。g表示新文本将替换所有匹配的文本。p表示打印与模式匹配的行的原先内容。w file表示将替换的结果写到文件file中。
xjhubuntu:~/iot/tmp$ cat data3.txt
This is a test of the test script.
This is the second test of the test script.
xjhubuntu:~/iot/tmp$ sed s/test/trial/ data3.txt #默认替换每行中第一处匹配
This is a trial of the test script.
This is the second trial of the test script.
xjhubuntu:~/iot/tmp$ sed s/test/trial/2 data3.txt #使用数字指定替换每行中第几处匹配
This is a test of the trial script.
This is the second test of the trial script.
xjhubuntu:~/iot/tmp$ sed s/test/trial/g data3.txt #使用g指定替换每行中所有的匹配
This is a trial of the trial script.
This is the second trial of the trial script.
xjhubuntu:~/iot/tmp$ sed s/test/trial/p data3.txt #使用p表示如果匹配则在输出替换后的内容之前先输出原先行的内容
This is a trial of the test script.
This is a trial of the test script.
This is the second trial of the test script.
This is the second trial of the test script.
xjhubuntu:~/iot/tmp$ sed -n s/test/trial/p data3.txt #-n选项表示禁止sed输出
This is a trial of the test script. #但p标志会输出修改后的行二者结合表示输出替换后的内容
This is the second trial of the test script.
xjhubuntu:~/iot/tmp$ sed s/test/trial/w test.txt data3.txt #使用w指定将替换后的结果输出到文件中
This is a trial of the test script.
This is the second trial of the test script.
xjhubuntu:~/iot/tmp$ cat test.txt
This is a trial of the test script.
This is the second trial of the test script.
xjhubuntu:~/iot/tmp$ 3通过行寻址可以让编辑器命令只作用于文本文件的某些特定的行。在sed编辑器中可以用数字来表示特定的行或者行区间或者可以用文本模式过滤出特定的行。注意下面的例子都是以s命令为例进行说明的但其实其他命令也适用。
【1】以数字来表示特定的行或者行区间使用格式如下。
sed 起始行号,结束行号s/pattern/replacement/ file
如果有多条命令则使用{ }将多条以分号隔开或者用次提示符的方式的命令包围起来。
注意sed编辑器将文本流中的第一行编号为1文本流最后一行用$来表示。
xjhubuntu:~/iot/tmp$ cat data4.txt
XJH is a boy.
XJH is a boy.
XJH is a boy.
XJH is a boy.
xjhubuntu:~/iot/tmp$ sed 2s/boy/girl/ data4.txt #替换第2行
XJH is a boy.
XJH is a girl.
XJH is a boy.
XJH is a boy.
xjhubuntu:~/iot/tmp$ sed 2 s/boy/girl/ data4.txt #有空格也行
XJH is a boy.
XJH is a girl.
XJH is a boy.
XJH is a boy.
xjhubuntu:~/iot/tmp$ sed 2,3s/boy/girl/ data4.txt #标准例子
XJH is a boy.
XJH is a girl.
XJH is a girl.
XJH is a boy.
xjhubuntu:~/iot/tmp$ sed 2,$s/boy/girl/ data4.txt #符号$表示文本最后一行
XJH is a boy.
XJH is a girl.
XJH is a girl.
XJH is a girl.
xjhubuntu:~/iot/tmp$ sed 2,3{s/boy/girl/;s/XJH/XJQ/} data4.txt #多条命令的写法1
XJH is a boy.
XJQ is a girl.
XJQ is a girl.
XJH is a boy.
xjhubuntu:~/iot/tmp$ sed 2,3{s/boy/girl/ s/XJH/XJQ/} data4.txt
XJH is a boy. #多条命令的写法2
XJQ is a girl.
XJQ is a girl.
XJH is a boy.
xjhubuntu:~/iot/tmp$ 【2】用文本模式过滤出特定的行使用格式如下。
sed /pattern/s/pattern_s/replacement/ file
如果有多条命令则使用{ }将多条以分号隔开或者用次提示符的方式的命令包围起来。
xjhubuntu:~/iot/tmp$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
xjhubuntu:~/iot/tmp$ sed -n /root/s/bash/sh/p /etc/passwd #添加-n选项与替换标志p只打印替换后的行内容
root:x:0:0:root:/root:/bin/sh
xjhubuntu:~/iot/tmp$ sed /root/s/bash/sh/ /etc/passwd #否则会打印全部内容
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
[……省略部分内容……]
xjhubuntu:~/iot/tmp$ sed /root/{s/bash/sh/;s/bin/usrbin/} /etc/passwd
root:x:0:0:root:/root:/usrbin/sh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
[……省略部分内容……]
xjhubuntu:~/iot/tmp$ sed -n /root/{s/bash/sh/p;s/bin/usrbin/} /etc/passwd
root:x:0:0:root:/root:/bin/sh
xjhubuntu:~/iot/tmp$ sed -n /root/{s/bash/sh/;s/bin/usrbin/p} /etc/passwd
root:x:0:0:root:/root:/usrbin/sh
xjhubuntu:~/iot/tmp$ sed -n /root/{s/bash/sh/p;s/bin/usrbin/p} /etc/passwd
root:x:0:0:root:/root:/bin/sh
root:x:0:0:root:/root:/usrbin/sh
xjhubuntu:~/iot/tmp$
2、删除特定行命令d
如果要删除文本流中的特定行可以使用删除命令d。
可以通过上面提到的两种行寻址方式来匹配要删除的某些行。
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 3d data4.txt
This is line number 1.
This is line number 2.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 2,3d data4.txt
This is line number 1.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 3,$d data4.txt
This is line number 1.
This is line number 2.
xjhubuntu:~/iot/tmp$ sed /number 3/d data4.txt
This is line number 1.
This is line number 2.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed /number 2/,/number 4/d data4.txt
This is line number 1.
xjhubuntu:~/iot/tmp$
3、插入命令i、附加命令a
插入命令iinsert的首字母会在指定行之前增加一个新行。
附加命令aappend的首字母会在指定行之后增加一个新行。
这两个命令的使用格式如下其中command为i或者afile表示要往哪个文件中插入或者附加如果缺省则默认插入或者附加到输入流中new_line表示要添加的内容而address表示往文件中的哪一行插入或者附加可以通过之前说的两种行寻址方式来确定这个地址。
sed [address]command\new_line file
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ echo Test line 2 | sed i\Test line 1
Test line 1
Test line 2
xjhubuntu:~/iot/tmp$ echo Test line 2 | sed a\Test line 1
Test line 2
Test line 1
xjhubuntu:~/iot/tmp$ sed i\Test line 1#因为缺省file则默认从输入流获取内容
Test line 2 #因此要输入一行内容并按回车
Test line 1 #接下来的两行是结果的显示。注意如果不按ctrld产生EOF符号会提示一直输入
Test line 2
xjhubuntu:~/iot/tmp$ sed 3i\Test line 1 data4.txt #在第3行前面插入一行
This is line number 1.
This is line number 2.
Test line 1
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 3a\Test line 1 data4.txt #在第3行后面插入一行
This is line number 1.
This is line number 2.
This is line number 3.
Test line 1
This is line number 4.
xjhubuntu:~/iot/tmp$ sed $a\Test line 1 data4.txt #符号$表示数据最后一行
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
Test line 1
xjhubuntu:~/iot/tmp$ sed /number 2/i\Test line 1 data4.txt
This is line number 1.
Test line 1
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed $i\Test line 1 data4.txt #使用文本模式的行寻址方式定位行
This is line number 1.
This is line number 2.
This is line number 3.
Test line 1
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 2i\ #在指定行前插入或者指定行后附加多行文本的方法this is a cat.\this is a dog. data4.txt
This is line number 1.
this is a cat.
this is a dog.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$
4、修改命令c
该命令可以修改数据流中整行文本的内容其用法与插入命令或者附加命令一致把i或者a改为c即可这里不再赘述。但值得注意的是如果在c命令中使用地址区间那么不会把该地址区间的每一行都改为新行而是用新的一行来替换这个地址区间里的所有行。
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed 2,3c\This is a cat. data4.txt
This is line number 1.
This is a cat.
This is line number 4.
xjhubuntu:~/iot/tmp$
5、转换字符y
转换字符y是唯一可以处理单个字符的sed编辑器命令使用格式如下
sed [address]y/inchar/outchar/ file
这个命令会将inchar中的第n个字符转换为outchar中的第n个字符直到处理inchar中的所有字符。使用该命令时要求inchar和outchar的长度一样否则会报错。
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed y/123/789/ data4.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
xjhubuntu:~/iot/tmp$
6、打印命令
在替换命令s中提到可以利用替换标志p来与-n选项来显示sed编辑器修改过的行。另外也有3个命令能够打印数据流中的信息。
1利用p命令打印行其最常见的用法是打印包含匹配文本模式的行这需要结合-n选项。
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ sed -n /number 3/p data4.txt
This is line number 3.
xjhubuntu:~/iot/tmp$ sed -n 2,3p data4.txt
This is line number 2.
This is line number 3.
xjhubuntu:~/iot/tmp$ sed -n /number 3/{ps/This/It/} data4.txt
This is line number 3. #如果不带p标志因为用了-n选项则修改后的内容不会被打印出来
xjhubuntu:~/iot/tmp$ sed -n /number 3/{ #这个例子表示利用打印命令在修改前查看原先行的内容ps/This/It/p} data4.txt
This is line number 3.
It is line number 3.
xjhubuntu:~/iot/tmp$
2利用等号命令打印行号即打印行在数据流中的当前行号。
xjhubuntu:~/iot/tmp$ sed data4.txt
1
This is line number 1.
2
This is line number 2.
3
This is line number 3.
4
This is line number 4.
xjhubuntu:~/iot/tmp$ sed -n /number 2/{p} data4.txt
This is line number 2.
2
xjhubuntu:~/iot/tmp$
3利用小写的L打印行该命令可以打印数据流中的文本和不可打印的ASCII字符。不可打印的ASCII字符在显示结果中一般以反斜杆加其八进制值或者以C语言风格的形式显示比如\t。
xjhubuntu:~/iot/tmp$ cat data5.txt
This is line number 1. #这里故意留了一行
xjhubuntu:~/iot/tmp$ sed -n l data5.txt
This\tis\tline\tnumber\t1.$
$
xjhubuntu:~/iot/tmp$xjhubuntu:~/iot/tmp$ cat data5.txt
This is line number 1. #这里的间隔是按了tab键产生的即\t这个制表符
This is line number 2.
xjhubuntu:~/iot/tmp$ sed -n l data5.txt
This\tis\tline\tnumber\t1.$ #\t表示制表符$表示换行符
This\tis\tline\tnumber\t2.$
xjhubuntu:~/iot/tmp$
7、写入命令w
该命令可以将一个文件中的某些行如果指定或者将标准输入中的内容默认情况写入到另一个文件中。如果是将文件中的某些行写入到另一个文件可以通过之前提到的两种行寻址方式查找到要写入的行。注意是将哪个文件中的内容写到哪一个另外的文件即注意格式。
xjhubuntu:~/iot/tmp$ sed 2,3 w test.txt data4.txt #将data4.txt文件中的第2到第3行的内容写到文件test.txt中
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ cat test.txt
This is line number 2.
This is line number 3.
xjhubuntu:~/iot/tmp$
可以利用该命令结合文本模式匹配方式从某个文件中提取想要的数据并写入到新文件中。
xjhubuntu:~/iot/tmp$ cat data6.txt
name age height
xjh 18 176
xjq 25 180
zzm 45 160
xjhubuntu:~/iot/tmp$ sed -n /xjh/w xjh.txt data6.txt
xjhubuntu:~/iot/tmp$ cat xjh.txt
xjh 18 176
xjhubuntu:~/iot/tmp$
8、读取命令r
该命令会将数字文件中的所有文本行都插到数据流中。注意是读取哪个文件的内容插到哪个另外的文件中即注意格式。
xjhubuntu:~/iot/tmp$ cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$ cat data5.txt
This is line number 1.
This is line number 2.
xjhubuntu:~/iot/tmp$ sed /number 2/r data5.txt data4.txt #将data5,.txt的内容插入到data4.txt中指定的行的后面
This is line number 1.
This is line number 2.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
xjhubuntu:~/iot/tmp$
该命令与删除命令配合使用可以利用另一个文件中的数据来替代某个文件中的占位文本即某个文本模板中的词语利用该模板时在该词语处添加内容。
xjhubuntu:~/iot/tmp$ cat name.txt
XJH
XJQ
ZZP
xjhubuntu:~/iot/tmp$ cat notice.txt
Would the following people:
LIST
Please stand up!
xjhubuntu:~/iot/tmp$ sed /LIST/{r name.txtd} notice.txt
Would the following people:
XJH
XJQ
ZZP
Please stand up!
xjhubuntu:~/iot/tmp$