sed常用命令

SED 常用command

p (sed –n)
 显示行
=
 显示行号
=在这里是command
g
 加空行
d
 删除行
/pattern/s/原串/替换串  
sed '/disable/s/yes/no/g'

s//n//
s/aaa/aaabbb/或s/aaa/&bbb
s/aaa//

 替换
把含有disable的行中所有(g) 的yes换成no

‘回车’换成‘空’,删除回车符,提行,合行
行内aaa后插入词bbb
行内删除词------就是”空词//”替换
/zzz/a /xxx/yyy

/zzz/a //

/zzz/a //yyy
在匹配行zzz,行内xxx后追加yyy

在匹配行zzz,另起一行追加空行

在匹配行zzz,另起一行追加yyy
/pattern/r 22.txt' 111.txt从文件22.txt读出,然后追加到111.txt





    Sed 最基本的sed –n  ‘p’功能=grep的功能
sed -n '/disable/p' 111.txt
  grep disable 111.txt
sed -n '/disable/!p' 111.txt  grep -v disable 111.txt

   
     sed –n ‘=’    表示” 显示行号,但不显示行内容
[macg@machome ~]$ sed -n '=' 111.txt
1
2
3
4
[macg@machome ~]$ sed -n '/disable/= ' 111.txt 
                 查找文件中含有disable的行的行号
3
9


    Sed ‘=’ file和sed –n ‘=’ file的区别
Sed –n ‘=’ 1.txt
只显示行号
sed = 1.txt 
显示行号,同时显示每行内容(没有-n)

 
        /pattern/s/原串 /替换串           查找并替换
# more 111.txt
this is 111file
this is 222file
        disable = yes
this is file
test1 = yes
test2 = yes 
[root@machome macg]#  sed '/disable/s/yes/no/g' 111.txt
this is 111file
this is 222file
        disable = no
this is file
test1 = yes
test2 = yes

 

  •     常见的用法是把/pattern/省略,即针对所有行 ,直接s打头——s/xxx/yyy/

 

#  sed 's/yes/no/' 111.txt
this is 111file
this is 222file
        disable = no
this is file
test1 = no
test2 = no

    s/xxx/yyy缺陷:替换每行只进行一次就停止,若要全部替换,要加参数g,s/xxx/yyy/g
$ sed s/echo/test/ ttt1
test  echo  echo
 标准替换s
只替换每一行中的第一个 “echo”字串
$ sed s/echo/test/g ttt1
test  test  test
 全部替换s/xxx/yyy/g
将每一行中的所有的echo都换成“test”
                  

    替换s,别忘了尾部的/
$ sed s/echo/test ttt1
sed: -e expression_r_r #1, char 11: unterminated `s' command
$ sed s/echo/test/ ttt1


    !s    不匹配的行,替换(不匹配,!加在comand上而不是pattern上)
sed '/baz/!s/foo/bar/g'    !s加/pattern/
不含有/pattern/的行替换

    ;     多重替换 (实际就是多语句,只不过用;加在一行里)
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'
不管是“scarlet”“ruby”还是“puce”,一律换成“red”


    s//n//  ‘回车’换成‘空’,删除回车符,提行,合行


    /pattern/a/xxx/yyy        在匹配pattern的行的xxx后面追加yyy
实际等于两个匹配条件:

 

  • 第一个匹配条件确定行
  • 第二个匹配条件确定行内的追加位置

sed "${LINENO}a/        FontPath     /"/usr/share/fonts/ttf/zh_CN//""


  /pattern/a//yyy   行尾追加(另起一行追加)更常用些
匹配pattern行,并且另起一行追加yyy

[macg@machome ~]$ sed '/^disable /a//this is a test' 111.txt  
将a//后的内容追加到disable打头 的行后面行(另起一行

this is 111file
this is 222file
        disable = yes        
本行是空格打头,不是disable打头 ,所以不匹配^disable

this is 333file
this is 444file
this is file
test1 = yes
test2 = yes
disable=no
 this is a test  
[macg@machome ~]$ sed '/disable /a//this is a test' 111.txt   
将a//后的内容追加到含有disable的行后面行(另起一行)

this is 111file
this is 222file
        disable = yes
 this is a test                
this is 333file
this is 444file
this is file
test1 = yes
test2 = yes
disable=no
 this is a test     
 


   Sed ‘3a//yyy’               匹配行号并追加
[macg@machome ~]$ sed '3a// this is a test' 111.txt
this is 111file
this is 222file
        disable = yes
 this is a test              
this is 333file
this is 444file
this is file
test1 = yes
test2 = yes
disable=no
 


   注意行尾追加命令a后面的格式,前面两个斜杠,后面无斜杠
sed '/^disable/a //this is a test' 111.txt  
sed '3a// this is a test' 111txt
下面是错误格式:
sed '3a/this is a test/' 111.txt


   前面说了行尾追加,行内追加(即行内修改)如何做?实际是采用替换s /&
[macg@machome ~]$ sed 's/disable/& this is test/' 111.txt
                 实际是用& this is test替换disable
                注意&即代表前面的disable
this is 111file
this is 222file
        disable this is test = yes
this is 333file
this is 444file
this is file
test1 = yes
test2 = yes
disable this is test=no
this is 343file
this is 344file 

  
行内追加的缺陷:如果用此法实现行尾追加,就必须把整个行不错一句的敲成pattern
[macg@machome ~]$ sed 's/disable = yes /& this is test/' 111.txt
this is 111file
this is 222file
        disable = yes this is test
既然是追加到行尾,还是用a//yyy更合理,s/&只是在行内替换时有效


   行内aaa后插入词bbb----------就是替换s/aaa/aaabbb/或s/aaa/&bbb


   行内删除词------就是”空词//”替换------s/aaa//


    r ------读文件 ,然后追加
另一种追加行, 从一个文件中读出的行,追加到另一个文件pattern后
[macg@machome ~]$ more 22.txt
222222222222file    
[macg@machome ~]$ more 111.txt
this is 111file
this is 222file
        disable = yes  
[macg@machome ~]$ sed '/disable/r 22.txt' 111.txt
this is 111file
this is 222file
        disable = yes
222222222222file   





=============================sed 数字行操作===================================

    代替/pattern/的另一种数字匹配句式-------匹配行号

 

  • 数字直接表示行号

所以不需要/pattern/
sed '1,10d'    删除文件中开头的10行
1,10 逗号是“从…到…”

  • 特殊符号表示行号

sed '$d'    删除文件中的最后一行
$表示最后一行

 
    常见的两种数字标识行号用法

  • 显示文件中的第几行

[root@machome macg]#  sed -n '2p' 111.txt
this is 222file

  • 显示多行

$ sed -n '2,7p' 111.txt     逗号2,7相当于from … to
this is 222file
        disable = yes
this is 333file
this is 444file
this is file
test1 = yes  

sed '2,$d' 111.txt    删除111.txt文件的第2行到末尾所有行


注意'2,7'与'2p;7p'的区别
'2,7p’表示2至7行
'2p;7p’显示2,7行


    Command ‘=’ 显示行号
$ sed -n '/disable/=' 111.txt| sed -n '$p
9                查找文件中含有disable的行的最后一行的行号


sed -n '$='   file   计算文件行数 (模拟 "wc -l")
$  -----------匹配文件最后一行
=  -----------显示当前行号
说是计算行号,实际是显示最后一行的行号

#  sed -n '$=' 111.txt
5    

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值