liunx之sed命令详解

来源:互联网 发布:php 统计在线人数代码 编辑:程序博客网 时间:2024/06/09 14:34

  • 简介
  • 使用语法
    • 常用选项option可选
    • 常用命令command
  • 实例
    • a 追加
        • 追加指定单行
        • 范围匹配追加
        • 追加指定正则匹配行
    • i 插入
    • c 取代
        • 取代指定单行
        • 范围匹配替换
        • 正则匹配替换
    • s 替换
        • 正则表达式后向引用
    • d 删除
        • 删除指定单行
        • 范围匹配删除
        • 正则匹配删除
    • p 打印
        • 范围匹配打印
        • 正则匹配打印

简介

sed全称是:Stream EDitor,是一个很好的文件处理工具

使用语法

sed [option] 'command' file_path

常用选项option(可选)

  • -i 直接修改读取的文件内容,而不是由屏幕输出。
  • -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令;
  • -n 使用安静(silent)模式。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;
  • -e 直接在指令列模式上进行 sed 的动作编辑;
  • -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);

常用命令command

  • [range]a string∶append即追加字符串string, a 的后面可以接字串string(多行字符串可以用\n分隔),而这些字串将追加到每个匹配行的下一行,range表范围,可以用数字区间(n[,m])表示,也可以用正则(/pattern/)

  • [range]i string∶insert即插入字符串, a 的后面可以接字串string(多行字符串可以用\n分隔),而这些字串将追加到每个匹配行的上一行,range表范围,可以用数字区间(n[,m])表示,也可以用正则(/pattern/)

  • [range]c string∶取代, c 的后面可以接字串(多行字符串可以用\n分隔),而这些字串将替换到匹配的行,range表范围,可以用数字区间(n[,m])表示,也可以用正则(/pattern/)

  • s: 替换,通常这个 s 的动作可以搭配正规表示法!例如:1,2s/old/new/g,将old字符串替换成new字符串

  • [range]d∶delete即删除,删除指定范围的内容。range表范围(不指定范围表示所有内容),可以用数字区间(n[,m])表示,也可以用正则(/pattern/)

  • [range]p∶print即打印,打印出指定范围的资料。通常 p 会与参数 sed -n 一起运作。range表范围(不指定范围表示所有内容),可以用数字区间(n[,m])表示,也可以用正则(/pattern/)

实例:

假设有一个本地文件test.txt,文件内容如下:

[root@localhost ~]# cat test.txtmy name is kwinmy email address is kwinwong@hotmail.commy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

a 追加

追加指定单行

[root@localhost ~]# sed '1a hello' test.txtmy name is kwinhellomy email address is kwinwong@hotmail.commy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用2,表示第二行到最后一行中间所有的行(包含第二行和最后一行)。

范围匹配追加

[root@localhost ~]# sed '1,$a hello' test.txtmy name is kwinhellomy email address is kwinwong@hotmail.comhellomy blog is http://blog.csdn.net/kwinhhelloPlease contact me if you have any questionshelloendhello

本例表示在所有的行后面都加上”hello”字符串,从输出可以看到效果。同sed 'a hello' test.txt

追加指定正则匹配行

[root@localhost ~]# sed '/^my.*com$/a hello' test.txtmy name is kwinmy email address is kwinwong@hotmail.comhellomy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

本例使用正则表达式匹配行,^my.*com$表示以my开头,以com结尾的行,则可以匹配到文件的”my email address is kwinwong@hotmail.com”这样,所以在该行后面增加了”hello字符串。

i 插入

同a追加,不同处是a是将字符串追击到在匹配的行的后一行,而i则是将字符串插入到匹配的行的前一行

c 取代

取代指定单行

取代第1行

[root@localhost ~]# sed '1c hello' test.txthellomy email address is kwinwong@hotmail.commy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用2,表示第二行到最后一行中间所有的行(包含第二行和最后一行)。

范围匹配替换

替换1到2行

[root@localhost ~]# sed '1,2c hello everyone' test.txthello everyonemy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

正则匹配替换

[root@localhost ~]# sed '/^my.*com$/c hello everyone' test.txtmy name is kwinhello everyonemy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

s 替换

1,2表示范围(默认所有行1,$),本例将文件中的所有kwin替换成kwinwong,最后的g是global的意思,也就是全局替换,如果不加g,则只会替换范围行内的每一行的第一个kwin。

[root@localhost ~]# sed '1,2s/kwin/kwinwong/g' test.txtmy name is kwinwongmy email address is kwinwongwong@hotmail.commy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

正则表达式后向引用

sed命令中使用()包裹的内容表示正则表达式的第n部分,序号从1开始计算,所以\1就是kwin

[root@localhost ~]# sed 's/\(kwin\)/\1wong/g' test.txtmy name is kwinwongmy email address is kwinwongwong@hotmail.commy blog is http://blog.csdn.net/kwinwonghPlease contact me if you have any questionsend

d 删除

删除指定单行

删除第1行

[root@localhost ~]# sed '1d hello' test.txtmy email address is kwinwong@hotmail.commy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用2,表示第二行到最后一行中间所有的行(包含第二行和最后一行)。

范围匹配删除

删除1到2行

[root@localhost ~]# sed '1,2d hello' test.txtmy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

正则匹配删除

[root@localhost ~]# sed '/^my.*com$/d' test.txtmy name is kwinmy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

p 打印

本例在屏幕上打印第三行到最后一行的内容,p命令一般和-n选项一起使用。

范围匹配打印

[root@localhost ~]# sed '3,$p' test.txt  -nmy blog is http://blog.csdn.net/kwinhPlease contact me if you have any questionsend

正则匹配打印

[root@localhost ~]# sed -n '/^my.*com$/p' test.txtmy email address is kwinwongwong@hotmail.com
0 0
原创粉丝点击