sed详解

-- 本文原创,允许转载

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数:
-n 使用静默模式,一般在sed的用法中,所有来自标准输入的内容都要print到屏幕,加-n后只显示经过sed处理的内容

-e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件,可接多个,但要分别对每个连续动作指定-e,一般只有一个月也可省略。
-f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件
-h或--help 显示帮助。
-n或--quiet或--silent 仅显示script处理后的结果。
-V或--version 显示版本信息。

-r sed 的动作支持的是扩展正规表示法的语法。(预设是基础正则表示法语法)

-i 直接在文件上修改,不显示在终端

动作:
a:新增, a 的后面可以string,这些string会在新的一行,即出现目前的下一行 c:取代, c 的后面可以string,这些字串可以取代 特定的某一行,也可替换n1,n2 之间的行 d:删除, d 因为是删除,所以 d 不接后续 i:插入, i 的后面可以string,而这些string会在新的一行出现,即目前的上一行 p:打印, 亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行 s:取代, 可以直接进行取代的工作,通常这个 s 的动作可以搭配正则表达式
PS:以上动作一般用作接收其他动作或命令送过来的数据流,做处理后显示出来。
实例: 原简单测试文件:
[gandefeng@localhost ~]$ cat test 
this
is
a
test

a 新增:

在第一行后面新增一行

[gandefeng@localhost ~]$ sed '1a this a new line' test 
this
this a new line
is
a
test

在最后一行新增一行

[gandefeng@localhost ~]$ sed '$a this a new line' test 
this
is
a
test
this a new line

i 插入
在第一行前插入,即插入的行为第一

[gandefeng@localhost ~]$ sed '1i this a new line' test 
this a new line
this
is
a
test

在最后一行插入,及插入的行为倒数第二行

[gandefeng@localhost ~]$ sed '$i this is a new line' test 
this
is
a
this is a new line
test

 

 c 取代替换

替换第二行

[gandefeng@localhost ~]$ sed '2c replace line 2' test 
this
replace line 2
a
test

 

 d 删除

删除第三行

[gandefeng@localhost ~]$ sed '3d' test 
this
is
test

 

 删除2-3行

[gandefeng@localhost ~]$ sed '2,3d' test 
this
test

 

 p 打印,一般和-n一起用

列出2和3行

[gandefeng@localhost ~]$ sed -n '2,3p' test 
is
a
[gandefeng@localhost ~]$ sed  '2,3p' test -n
is
a

 

列出不连续的行,如列出第二行和第四行

[gandefeng@localhost ~]$ sed -e '2p'  -e '4p' test -n
is
test

 

 s 搜索匹配(/pattern/),配合正则表达式

搜索this并在其后添加string (注意&的使用位置,和加\转义)

[gandefeng@localhost ~]$ sed 's/this/&string/g' test 
thisstring
is
a
test

 

 搜索this并在其后添前string

[gandefeng@localhost ~]$ sed 's/this/string&/g' test 
stringthis
is
a
test

 

 转义&和\ ,只转义&则表示&为普通字符,后面的字符串一起变成了替换搜索匹配到的字符串,而把\本身转义以后则表示string\整体为一个要添加的字符串

[gandefeng@localhost ~]$ sed 's/this/string\&/g' test 
string&
is
a
test
[gandefeng@localhost ~]$ sed 's/this/string\\&/g' test 
string\this
is
a
test

 

 g 的含义,加g表示对匹配到的每一行的每一处做出语句中的处理动作,否则只处理匹配到的行的第一处,例如:在t后面加数字4

[gandefeng@localhost ~]$ sed 's/t/&4/' test 
t4his
is
a
t4est
[gandefeng@localhost ~]$ sed 's/t/&4/g' test 
t4his
is
a
t4est4

 

 还可以对要处理的行做出限定,例如只对前三行做处理;

[gandefeng@localhost ~]$ sed '1,3 s/t/&4/g' test 
t4his
is
a
test

 

 对第一行和第四行分别做出不同的处理

[gandefeng@localhost ~]$ sed -e '1 s/t/&4/g' -e '4 s/t/&6/g'  test 
t4his
is
a
t6est6

 

 匹配带t的行,并删除

[gandefeng@localhost ~]$ sed '/t/d' test 
is
a

 

 为每一行或者指定行的行首添加#号

[gandefeng@localhost ~]$ sed '1,3 s/^/#/g' test
#this
#is
#a
test
[gandefeng@localhost ~]$ sed 's/^/#/g' test
#this
#is
#a
#test

 

 行尾同理

[gandefeng@localhost ~]$ sed 's/$/#/g' test
this#
is#
a#
test#

 

 删除行首#号

[gandefeng@localhost ~]$ cat test 
#this
#is
#a
#test
[gandefeng@localhost ~]$ sed 's/^#//g' test 
this
is
a
test

 

 先精确匹配某一行,然后再对该行做替换处理

[gandefeng@localhost ~]$ cat test 
this
is
a
test
[gandefeng@localhost ~]$ sed '/this/s/th/TH/g' test 
THis
is
a
test

 

 

 

 

 

 

其他:

1,删除行首空格

sed 's/^[ ]*//g' filename

sed 's/^ *//g' filename

sed 's/[[:space:]]*//g' filename

2,使用变量替换

sed -e "s/$var1/$var2/g" filename

3,删除文本中空行和空格组成的行及#号注释的行

grep -v ^# filename | sed /^[[:spacce:]]*$/d | sed /^$/d

 

应用:如

对secure日志按时间抓取

less /var/log/secure |  sed -n '/12:48:48/,/12:48:55/p'

对ip的抓取

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.100.103  netmask 255.255.255.0  broadcast 172.16.100.255
        inet6 fe80::20c:29ff:fef9:8d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f9:00:8d  txqueuelen 1000  (Ethernet)
        RX packets 48974  bytes 33927576 (32.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34246  bytes 5032445 (4.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[gandefeng@localhost ~]$ ifconfig eno16777736|grep 'inet '|sed 's/^.*inet //g'|sed 's/netmask.*//g'
172.16.100.103  

 

 

 

直接修改原文件内容(危险动作) 加-i 其他配合动作如上,不再赘述

[gandefeng@localhost ~]$ cat test 
this
is
a
test
[gandefeng@localhost ~]$ sed -i 's/^/#/g' test 
[gandefeng@localhost ~]$ cat test 
#this
#is
#a
#test

 

 

 

 

 

 

 

 


 

posted @ 2017-05-13 13:42  Dothraki  阅读(629)  评论(0编辑  收藏  举报