linux文本三剑客演讲稿,linux文本处理三剑客-sed

sed 是什么?

sed是一种流编辑器,它是文本处理中非常中的工具,在linux中被称为linux文本处理三剑客之一,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处

理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

sed工作模型

3c8ce6380daa33c267ccb0e2e66a02de.png

sed的基本语法

sed [OPTION]... {script-only-if-no-other-script} [input-file]..

opotion 的选项:

-n 静默模式,不输出模式空间的内容

[root@localhost ~]#sed '1p' /etc/issue

\S

\S

Kernel \r on an \m

[root@localhost ~]#sed -n '1p' /etc/issue

\S

-e script -e script  同时执行多个脚本

[root@localhost ~]#sed -e '1s/bash$/zsh/' -e 's/^root/toor/'  /etc/passwd

toor:x:0:0:root:/root:/bin/zsh

-f /path/to/script   读取文件中的sed命令

[root@localhost ~]#cat p.sed

1p

2p

$p

[root@localhost ~]#sed -n -f p.sed

/etc/passwd root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

apache:x:1008:1008::/home/apache:/sbin/nologin

-i 直接修改原文件,慎用

[root@localhost ~]#sed -n '/^zgx/p' /etc/passwd

zgx:x:1007:1007::/home/zgx:/bin/bash

[root@localhost ~]#sed -i '/^zgx/d' /etc/passwd

[root@localhost ~]#sed -n '/^zgx/p' /etc/passwd

-r 使用扩展正则表达式

[root@localhost ~]#sed -n '/^ro\\+t/p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]#sed -n '/^ro+t/p' /etc/passwd

[root@localhost ~]#sed -n -r '/^ro+t/p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

script的表示方法

address+command

address:

1、startline,endline   指定范围

[root@localhost ~]#sed -n '1,2p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

2、/regexp/             正则匹配

[root@localhost ~]#sed -n '/^root/p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

3、/pattern1/,/pattern2/   正则匹配范围

[root@localhost ~]#sed -n '/^root/,/^lp/p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

4、linenumber  指定行

[root@localhost ~]#sed -n '10p' /etc/passwd

operator:x:11:0:operator:/root:/sbin/nologin

5、startline,+n  指定起始及向后行数

[root@localhost ~]#sed -n '10,+2p' /etc/passwd

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost ~]#sed -n '/^root/,+2p' /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

6、步进行

[root@localhost ~]#sed -n '1~2p' /etc/passwd   #输出奇数行

root:x:0:0:root:/root:/bin/bash

daemon:x:2:2:daemon:/sbin:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

command:

d 删除符合条件的行

[root@localhost ~]#sed '1d' /etc/passwd  #删除了第一行的root用户

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

p 显示符合条件的行

[root@localhost ~]#sed '1p' /etc/issue  #模式空间与匹配到的行都输出

\S

\S

Kernel \r on an \m

a \string 在指定的行后面追加新行

[root@localhost ~]#sed '1a\newline' /etc/issue

\S

newline

Kernel \r on an \m

i \string  在指定的行前面插入新行

[root@localhost ~]#sed '1i\newline' /etc/issue

newline

\S

Kernel \r on an \m

c \string 将匹配到的行替换为此处指定的文本text

[root@localhost ~]#sed '1c\newline' /etc/issue

newline

Kernel \r on an \m

r file 读取文件,合并文本

[root@localhost ~]#sed '1r /etc/issue' /etc/issue

\S

\S

Kernel \r on an \m

Kernel \r on an \m

w file 将地址指定的访问的行另存为指定文件中

[root@localhost ~]#sed '1,2 w /tmp/passwd'

/etc/passwd root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost ~]#cat /tmp/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

= 为模式匹配到的行打印出行数

[root@localhost ~]#sed '1,2=' /etc/passwd

1

root:x:0:0:root:/root:/bin/bash

2

bin:x:1:1:bin:/bin:/sbin/nologin

! 取反

[root@localhost ~]#sed -n '1!p' /etc/issue

Kernel \r on an \m

s/pattern/string/修饰符:查找并替换,默认只替换每一行被匹配到的字符串

修饰符:

g:全局替换

[root@localhost ~]#cat root.txt

root

root

txt

txt

test

test

ROOT

[root@localhost ~]#sed  's/^root/toor/g' root.txt

toor

toor

txt

txt

test

test

ROOT

i 忽略带下写

[root@localhost ~]#sed  's/^root/toor/i' root.txt

toor

toor

txt

txt

test

test

toor

w /path/to/somefile 将替换成功的结果保存至指定文件中

[root@localhost ~]#sed  's/^root/toor/w /tmp/root.txt' root.txt

toor

toor

txt

txt

test

test

ROOT

[root@localhost ~]#cat /tmp/root.txt

toor

toor

p 显示替换成功的行

[root@localhost ~]#sed -n 's/^root/toor/p' root.txt

toor

toor

高级编辑命令(不常用)

h:表示把模式空间的内容保存至保持空间

H:把模式空间中的内容追加至保持空间

g:把保持空间的内容覆盖至模式空间

G:把保持空间的内容追加至模式空间

x:把模式空间的内容与保持空间中内容互换

n:读取匹配到行的下一行至模式空间

N: 追加读取匹配到的行的下一行至模式空间中

D:删除多行模式空间中所有行

实例:

sed -n 'n;p' file 显示偶数行

sed '1!G;h;$!d'  file 逆序显示文件

sed '$!d' file 取出最后一行

sed  '$!N;$!D' FILE 取出最后两行

sed '/^$/d;G' file  删除所以空白行,为每行加空白行

sed 'n;d' file:显示奇数行

原创文章,作者:N25_随心,如若转载,请注明出处:http://www.178linux.com/67014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值