sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
sed命令行格式为:
sed [-nefri] ‘command‘ filename
常用选项:
-n:使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。
但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e:直接在指令列模式上进行 sed 的动作编辑;
-f: 直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
-r: sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i: 直接修改读取的档案内容,而不是由屏幕输出。
常用命令:
a:新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c:取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d:删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p:列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s:取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
举例:(假设我们有一文件名为sed.txt)
# sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,Welcome to my blog.
删除某行(对输入流进行操作--input stream)
[[email protected] tmp]# sed ‘1d‘ sed.txt # 删除第一行 ruby is me,welcome to my blog. endruby is me,Welcome to my blog. [[email protected] tmp]# sed ‘$d‘ sed.txt # 删除最后一行 Hello! ruby is me,welcome to my blog.end [[email protected] tmp]# sed ‘2,$d‘ sed.txt # 删除第二行到最后一行 Hello! [[email protected] tmp]#
显示某行
[[email protected] tmp]# sed -n ‘1p‘ sed.txt # 显示第一行 Hello! [[email protected] tmp]# sed -n ‘2,$p‘ sed.txt # 显示第二行到最后一行 ruby is me,welcome to my blog. endruby is me,Welcome to my blog.
模式查询
[[email protected] tmp]# sed -n ‘/ruby/p‘ sed.txt # 查询包括关键字ruby的所有行 ruby is me,welcome to my blog.ruby is me,Welcome to my blog. [[email protected] tmp]# sed -n ‘/^r.*g.$/p‘ sed.txt # 查询以字母‘r‘开头,‘g.‘结尾的行 ruby is me,Welcome to my blog. [[email protected] tmp]#
增加一行或多行字符串
[[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]# sed ‘1a drink tea‘ sed.txt Hello! drink tea ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]# sed ‘1,3a drink tea‘ sed.txt Hello! drink tea ruby is me,welcome to my blog. drink tea end drink tea ruby is me,welcome to my blog. [[email protected] tmp]# sed ‘1,3a drink tea\nor coffee‘ sed.txt Hello! drink tea or coffee ruby is me,welcome to my blog. drink tea or coffee end drink tea or coffee ruby is me,welcome to my blog. [[email protected] tmp]# cat sed.txt # 说明操作的是输入流 Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]#
替换一行或多行
[[email protected] tmp]# sed ‘1c Hill‘ sed.txt # 第一行替换为Hill Hill ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]# sed ‘1,2c Hill‘ sed.txt # 第一行到第二行替换为Hill Hill end ruby is me,welcome to my blog. [[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog.
替换一行中的某部分
格式:sed ‘s/要替换的字符串/新的字符串/g‘ (要替换的字符串可以用正则表达式)
[[email protected] tmp]# sed -n ‘/rub/p‘ sed.txt | sed ‘s/ruby/bird/‘ # 替换ruby为bird bird is me,welcome to my blog. bird is me,welcome to my blog. [[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]# sed -n ‘/ruby/p‘ sed.txt | sed ‘s/ruby//‘ # 删除ruby is me,welcome to my blog. is me,welcome to my blog.
插入(对源文件进行的操作)
[[email protected] tmp]# cat sed.txt # 在文件sed.txt中最后一行直接输入"bye" Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. [[email protected] tmp]# sed -i ‘$a bye‘ sed.txt [[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. bye [[email protected] tmp]#
删除匹配行(对源文件进行的操作)
格式: sed -i ‘/匹配字符串/d‘ filename (注:若匹配字符串是变量,则需要"",而不是‘‘)
[[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. end ruby is me,welcome to my blog. bye [[email protected] tmp]# sed -i ‘/en/d‘ sed.txt [[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. ruby is me,welcome to my blog. bye [[email protected] tmp]#
替换匹配行中的某个字符串(对源文件进行的操作)
格式: sed -i ‘/匹配字符串/s/替换源字符串/替换目标字符串/‘ filename
[[email protected] tmp]# cat sed.txt Hello! ruby is me,welcome to my blog. ruby is me,welcome to my blog. bye [[email protected] tmp]# sed -i ‘/ru/s/ruby/birds/‘ sed.txt [[email protected] tmp]# cat sed.txt Hello! birds is me,welcome to my blog. birds is me,welcome to my blog. bye [[email protected] tmp]#