Linux三剑客之sed流编辑器.docx

上传人:b****5 文档编号:12535486 上传时间:2023-04-20 格式:DOCX 页数:55 大小:32.35KB
下载 相关 举报
Linux三剑客之sed流编辑器.docx_第1页
第1页 / 共55页
Linux三剑客之sed流编辑器.docx_第2页
第2页 / 共55页
Linux三剑客之sed流编辑器.docx_第3页
第3页 / 共55页
Linux三剑客之sed流编辑器.docx_第4页
第4页 / 共55页
Linux三剑客之sed流编辑器.docx_第5页
第5页 / 共55页
点击查看更多>>
下载资源
资源描述

Linux三剑客之sed流编辑器.docx

《Linux三剑客之sed流编辑器.docx》由会员分享,可在线阅读,更多相关《Linux三剑客之sed流编辑器.docx(55页珍藏版)》请在冰豆网上搜索。

Linux三剑客之sed流编辑器.docx

Linux三剑客之sed流编辑器

sed:

(流编辑器),全称是:

StreamEditor。

sed命令的功能同awk类似,差别在于,sed简单,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大.

sed是行编辑器(全屏编辑器有:

vi)。

sed是非交互式的编辑器。

默认它不会修改文件,除非使用shell重定向来保存结果。

默认情况下,所有的输出行都被打印到屏幕上。

sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。

具体过程如下:

首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。

sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。

处理完输入文件的最后一行后,sed便结束运行。

sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。

sed基本用法:

sed‘AddressCommand’file………..(sed‘指定地址指定编辑命令’文件名1文件名2………….)

指定地址就是对哪些行进行处理。

注意:

指定地址和指定编辑命令之间不需要空格。

意思就是指对文件1中的行执行编辑命令。

sed[选项]‘AddressCommand’file……

选项可有可不有,其中:

-n:

表示静默模式,表示不显示模式空间中的内容,指显示命令需要的,包括去掉重复的。

-i:

直接修改原文件,注意使用。

-e:

script–escript可以同时执行多个脚本。

-f:

sed_script。

sed–f/root/scriptccaa.txt

意思是说:

把/root/scriptcc里的脚本一个一个的用在aa.txt文件上。

-r:

表示使用扩展正则表达式。

指定地址Address包括:

1.起始行,结束行(startline,endline中间用逗号隔开,指从哪行到哪行)

如:

1,100表示从第一行到第100行。

给一个数值如5表示第五行。

[root@entestold]#sed-n"5p"aa(显示aa文件中的第五行,不加-n全显示第五行显示2遍)

hello!

[root@entestold]#sed-n"1,3p"aa(显示aa文件1到3行)

/thisisaa

hahahahavewoshihan.

#ZHANGDAWEITHISISDAXIE!

[root@entestold]#

2.模式(模式指正则表达式,regexp)(用两斜线/正则表达式/)

如:

/^root/找以root开头的行。

就是找文件中所有以root开头的行。

模式匹配需要两个斜线引起来,如/goo/。

[root@entestold]#sed-n"/^app/p"aa(查找以app开头的行)

appleismyfavoritefood.

[root@entestold]#sed-n"/^#/p"aa(查找以#号开头的行)

#ZHANGDAWEITHISISDAXIE!

#ok!

thINK.

#zheshisuojin!

[root@entestold]#

[root@entestold]#sed-n"/^$/p"aa(查找空白行)

[root@entestold]#

[root@entestold]#sed-n"/\!

$/p"aa(查找以!

号为结尾的行)

#ZHANGDAWEITHISISDAXIE!

hello!

findthisisok!

[root@entestold]#

3.模式1,模式2(表示第一次被模式1匹配的行开始到第一次被模式2匹配的行结束)

4.$(表示文件中最后一行)

$-1:

倒数第二行

[root@entestold]#sed-n"\$p"aa(显示最后一行)

theokisnotok!

hello*yes$or#!

[root@entestold]#

[root@entestold]#sed-n'\$p'aa(注意:

用单引号无效)

sed:

-eexpression#1,char3:

unterminatedaddressregex

[root@entestold]#

5.起始行,+n

表示从起始行往后加多少行。

包括当前行加上指定行,一共是n+1注意。

如5,+6表示从第五行开始加上6行。

指定编辑命令Command包括:

1.d:

删除符合条件的行。

例子:

[root@entestold]#catb

thisisb

hello

zhelishisan

shisihangya

[root@entestold]#sed'1,2d'b(删除1和2行,注意:

并没有删除源文件)

zhelishisan

shisihangya

[root@entestold]#

[root@entestold]#cat-nb

1thisisb

2hello

3zhelishisan

4shisihangya

[root@entestold]#sed'3,$d'b(删除第三行到最后一行)

thisisb

hello

[root@entestold]#

[root@entestold]#sed"3,$d"b(注意:

双引号错误)

sed:

-eexpression#1,char2:

unexpected`,'

[root@entestold]#cat-nb

1thisisb

2hello

3zhelishisan

4shisihangya

[root@entestold]#sed'/o/d'b(删除包含o的行,注意是删除该行)

thisisb

zhelishisan

shisihangya

[root@entestold]#

[root@entestold]#cat-nb

1thisisb

2hello

3zhelishisan

4shisihangya

[root@entestold]#sed'1,+2d'b(删除1+2行,一共删除3行)

shisihangya

[root@entestold]#

[root@entestold]#sed'1d'b(删除第1行)

hello

zhelishisan

shisihangya

[root@entestold]#sed'2d'b(删除第2行)

thisisb

zhelishisan

shisihangya

[root@entestold]#

[root@entestold]#catb

thisisb

hello

zhelishisan

shisihangya

tmpfs/dev/shmtmpfsdefaults00

devpts/dev/ptsdevptsgid=5,mode=62000

sysfs/syssysfsdefaults00

proc/procprocdefaults00

/etc/fstab

/dev/pts

[root@entestold]#sed'/^\//d'b(删除以/开头的行)

thisisb

hello

zhelishisan

shisihangya

tmpfs/dev/shmtmpfsdefaults00

devpts/dev/ptsdevptsgid=5,mode=62000

sysfs/syssysfsdefaults00

proc/procprocdefaults00

[root@entestold]#

2.p:

显示符合条件的行,-n‘5p’。

显示aa文档的第8行:

[root@entestold]#sed-n'8p'aa

findthisisok!

[root@entestold]#

显示aa文档的最后一行:

[root@entestold]#sed-n'$p'aa

/dev/pts

[root@entestold]#

显示aa文档8到10行:

[root@entestold]#sed-n'8,10p'aa

findthisisok!

zheshishouhang.

zhangdawei

[root@entestold]#

显示以/线开头的行:

[root@entestold]#sed'/^\//p'b(注意没有-n的显示,符合条件的显示2次默认)

thisisb(匹配的显示2次,不匹配的显示1次)

hello

zhelishisan

shisihangya

tmpfs/dev/shmtmpfsdefaults00

devpts/dev/ptsdevptsgid=5,mode=62000

sysfs/syssysfsdefaults00

proc/procprocdefaults00

/etc/fstab

/etc/fstab

/dev/pts

/dev/pts

说明:

’/^//p’同//引起来,加\转义符,就是//p---///p--/^\/p了。

[root@entestold]#sed-n'/^\//p'b(加选项-n的显示,表示不显示模式空间中的内容)

/etc/fstab(并且只显示匹配的行)

/dev/pts

[root@entestold]#

查找匹配boot的行:

[root@entestold]#catb1

thisisb1

dsvdsvsdsdsdboot

bin

bootsdsdsdfsdsds

devBOOT

[root@entestold]#sed-n'/boot/p'b1

dsvdsvsdsdsdboot

bootsdsdsdfsdsds

[root@entestold]#

查找有is的行:

[root@entestold]#sed-n'/is/p'b1

thisisb1!

thisistestlianxi!

ok!

thankis.

[root@entestold]#

3.a\string(a空格\内容)

表示在指定的行后面追加新行,内容为“string”。

在b文档中第二行加入#shuomingshuo:

[root@entestold]#sed"2a\#shuomingshuo"b(格式:

行a空格斜线要加入的东西)

thisisb

hello

#shuomingshuo

zhelishisan

[root@entestold]#

把xinjiaru加入b文档的第二行中:

[root@entestold]#cat-nb|sed“2axinjiaru”

1thisisb

2hello

xinjiaru

3zhelishisan

[root@entestold]#

在以/开头的行下加入#hellozhang:

[root@entestold]#sed'/^\//a\#hellozhang'b

thisisb

hello

zhelishisan

shisihangya

tmpfs/dev/shmtmpfsdefaults00

devpts/dev/ptsdevptsgid=5,mode=62000

sysfs/syssysfsdefaults00

proc/procprocdefaults00

/etc/fstab

#hellozhang

/dev/pts

#hellozhang

[root@entestold]#

[root@entestold]#sed'/^\//a987654321111111111111111111111111111'b(以/开头下行加)

/etc/fstab

987654321111111111111111111111111111

/dev/pts

987654321111111111111111111111111111

[root@entestold]#

4.i\string(i空格\内容)

表示在指定的行前面追加新行,内容为“string”。

[root@entestold]#sed'/^\//i\#hellozhang'b(在指定/前加入#hellozhang)

thisisb

hello

zhelishisan

shisihangya

tmpfs/dev/shmtmpfsdefaults00

devpts/dev/ptsdevptsgid=5,mode=62000

sysfs/syssysfsdefaults00

proc/procprocdefaults00

#hellozhang

/etc/fstab

#hellozhang

/dev/pts

[root@entestold]#

在第二行前面加入#shuomingshuo:

[root@entestold]#sed"2i\#shuomingshuo"b

thisisb

#shuomingshuo

hello

zhelishisan

[root@entestold]#

5.rfile(r空格文件路径)用于让某个文件内容显示在某个文件中部位置

将指定的文件内容添加到符合条件的行处。

(2,5r表示在第2行和第五行下面都显示)

[root@entestold]#catb1

thisisb1

dsvdsvsdsdsd

bin

bootsdsdsdfsdsds

dev

[root@entestold]#catb2

thisisb

hello

zhelishisan

shisihangya

/etc/fstab

/dev/pts

[root@entestold]#sed'2r/root/testold/b2'b1(把b2文件内容添加到b1文件中第2行下面)

thisisb1

dsvdsvsdsdsd

thisisb

hello

zhelishisan

shisihangya

/etc/fstab

/dev/pts

bin

bootsdsdsdfsdsds

dev

[root@entestold]#

6.wfile(w空格文件名)

将指定文件范围内的内容另存至指定的文件中。

[root@entestold]#cataa

thisisaa

hahahahaha

[root@entestold]#sed'/ha/w/root/ha.txt'aa(将aa文件中以ha开头的行保存到ha.txt中)

thisisaa

hahahahaha(可用-n不显示下面内容)

[root@entestold]#cat/root/ha.txt

hahahahaha

[root@entestold]#

注意:

如果文件有内容将被覆盖,需要注意。

7.s///查找并替换,默认替换每一行中的第一个字符串。

s@@@或s###都可以,当查找有/时,可用s###代替s///,不止可以只用斜线。

用法s/tt/uu/:

表示将指定的tt替换成uu。

注意:

tt可用正则表达式,uu不可以。

[root@entestold]#cataa

/thisisaa

hahahahavewoshihan.

/etc/httpd

[root@entestold]#sed's/h/H/'aa(将每行的第一个字符h替换成大写H)

/tHisisaa

Hahahahavewoshihan.

/etc/Httpd

[root@entestold]#sed's/^\//#/'aa(把aa文件中/更改为#)

#thisisaa

hahahahavewoshihan.

#etc/httpd

[root@entestold]#

注意替换区别:

[root@entestold]#cat-nc

1/tmpfs/dev/shmtmpfs

2devpts/dev/ptsdevpts

3sysfs/sys/sysfs

4proc/procproc

5/etc/fstab

6/dev/pts

[root@entestold]#

[root@entestold]#sed's#/#?

#'c(把每行开头的/替换成?

?

tmpfs/dev/shmtmpfs

devpts?

dev/ptsdevpts

sysfs?

sys/sysfs

proc?

procproc

?

etc/fstab

?

dev/pts

[root@entestold]#

[root@entestold]#sed's#^/#?

#'c(把以/字符开头的行替换成?

?

tmpfs/dev/shmtmpfs

devpts/dev/ptsdevpts

sysfs/sys/sysfs

proc/procproc

?

etc/fstab

?

dev/pts

[root@entestold]#sed's#/#?

#g'c(加g,把所有/替换成?

?

tmpfs?

dev?

shmtmpfs

devpts?

dev?

ptsdevpts

sysfs?

sys?

sysfs

proc?

procproc

?

etc?

fstab

?

dev?

pts

[root@entestold]#

如果每行中匹配的都想替换:

需要加修饰符。

i:

查找时忽略字符大小写

g:

全局替换

s/tt/uu/g:

表示每行所有tt替换成uu。

[root@entestold]#sed's/\//#/g'aa(把以/开头的替换成#)

#thisisaa

hahahahavewoshihan.

#etc#httpd#usr#dev

[root@entestold]#sed's/h/H/g'aa(把小h替换成大H)

/tHisisaa

HaHaHaHavewosHiHan.

/etc/Httpd/usr/dev

[root@entestold]#

[root@entestold]#catop

woshizhang1a.

woshizhang2a.

woshizhang3a.

[root@entestold]#sed's/.$/?

/g'op(把最后的点替换成问号)

woshizhang1a?

woshizhang2a?

woshizhang3a?

[root@entestold]#

查找替换中&的应用:

把like改为liker和把love改为lover:

[root@entestold]#catlo

hello,like

hi,mylove

[root@entestold]#sed's#l..e#&r#g'lo(s#要替换的#&替换的#,用&引用前面的)

hello,liker

hi,mylover

[root@entestold]#

注意:

这里不能用[root@entestold]#sed's#l..e#l..er#g'lo(要用&符号代替同字符)

hello,l..er

hi,myl..er(显示错误)

比如:

[root@entestold]#catop

woshizhang1a

woshizhang2a

woshizhang3a

[root@entestold]#sed's/z.....a/&ma/g'op

woshizhang1ama

woshizhang2ama

woshizhang3ama

[root@entestold]#

把后面l换成大写L:

[root@entestold]#catlo

hello,like

hi,mylove

[root@entestold]#sed's#l..e#L..e#g'lo(错误的)

hello,L..e

hi,myL..e

[root@entestold]#sed's#l\(..e\)#L\1#g'lo(把除了l以外的括起来,换成L\1是数字一)

hello,Like

hi,myLove

[root@entestold]#

[root@entestold]#catb1

thisisb1

dsvdsvsdsdsdboot

bin

bootsdsdsdfsdsds

devBOOT

[root@entestold]#sed's#d##g'b1(把d替换成空字符,什么都没有)

thisisb1

svsvsssboot

bin

bootsssfsss

evBOOT

[root@entestold]#

例子:

把命令history前面的空白字符去掉:

[root@entestold]#history

54find./-perm/022-ls

55find./-perm-022-ls

56mkdirquanxian

……………………………………….

[root@entestold]#history|sed's#^[[:

space:

]]*##g'

或:

[root@entestold]#history|sed's#^*##g'(前面只有一个空开字符,多个就无效)

[root@entestold]#history|sed's#^[[:

space:

]]*##g'|cut-d''-f1

1

2

3

………………….

[root@entestold]#

sed练习题:

1.删除/etc/grub.conf文件中行首的空白字符:

[root@entestold]#catgrub.conf

………………………………..

titleCentOS(2.6.32-431.el6.x86_6

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1