SECTION 23 sed编辑器基础


前节介绍了sed的简单使用方法,本节将介绍一些基本命令和功能

sed

1. 基本语法

sed [options] {sed-commands} [input-file]...  

option参数:

  • -f:把所有脚本放到一个script文件里使用
    sed -f <sed file> <file name>
    
    在这里插入图片描述
  • -e: 执行多条sed命令, 分号隔开
    # sed -e '<script 1> ; <script 2>' <file name>  
    
    在这里插入图片描述
    sed -n -e '/^root/ p' -e '/^nobody/ p' /etc/passwd
    
  • -n: 默认输出所有行,-n只输出匹配的行
  • -i: 直接修改文件内容;
  • -r: 支持扩展表达式

2. sed常用命令

 a\ 在当前行下面插入文本;

 i\ 在当前行上面插入文本;

 c\ 把选定的行改为新的文本;

 d 删除,删除选择的行;

 D 删除模板块的第一行;

 s 替换指定字符;

 h 拷贝模板块的内容到内存中的缓冲区;

 H 追加模板块的内容到内存中的缓冲区;

 g 获得内存缓冲区的内容,并替代当前模板块中的文本;

 G 获得内存缓冲区的内容,并追加到当前模板块文本的后面;

 l 列表不能打印字符的清单;

 n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令;

 N 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码;

 p 打印模板块的行。 P(大写) 打印模板块的第一行;

 q 退出Sed;

 b lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾;

 r file 从file中读行;

 t label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;

 T label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;

 w file 写并追加模板块到file末尾;

 W file 写并追加模板块的第一行到file末尾;

 ! 表示后面的命令对所有没有被选定的行发生作用;

 = 打印当前行号;

 # 把注释扩展到下一个换行符以前;

3. sed替换标记

 g 表示行内全面替换;

 p 表示打印行;

 w 表示把行写入一个文件;

 x 表示互换模板块中的文本和缓冲区中的文本;

 y 表示把一个字符翻译为另外的字符(但是不用于正则表达式);
 
 \1 子串匹配标记;

 & 已匹配字符串标记;

4. sed元字符集

 ^ 匹配行开始,如:/^sed/匹配所有以sed开头的行;
 
 $ 匹配行结束,如:/sed$/匹配所有以sed结尾的行;
 
 . 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d;
 
 * 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行;
  
 [] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed;
   
 [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行;
  
 \(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers;
  
 & 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**;
  
 \< 匹配单词的开始,如:/\ 
 \> 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行;
 
 x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行;
 
 x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行;
 
 x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行;

4. 更多的替换选项

替换(substitute)命令s用来在行中替换文本,默认会替换每行中第一次出现匹配的位置
让替换命令能够替换一行中不同地方出现的文本,需要使用替换标记(substitution flag),替换标记会替换命令字符串之后的设置,格式:
s/pattern/replacement/flags
替换标记有4种:

数字 表明新文本将替换第几处模式匹配的地方
g 表明新文本将会替换所有匹配的文本
p 表明修改过的行要打印出来
w file 将替换的结果写到文件中

示例1:

]# cat text1.txt 
1the fox, 2the fox, 3the fox ,4the fox
1the fox, 2the fox, 3the fox ,4the fox
]# sed 's/fox/dog/2' text1.txt 
1the fox, 2the dog, 3the fox ,4the fox
1the fox, 2the dog, 3the fox ,4the fox

示例2:

]# sed 's/fox/dog/g' text1.txt 
1the dog, 2the dog, 3the dog ,4the dog
1the dog, 2the dog, 3the dog ,4the dog

示例3:

cat text1.txt 
1the cat, 2the cat, 3the cat ,4the cat
1the fox, 2the fox, 3the fox ,4the fox
]# sed -n 's/cat/dog/p' text1.txt 
1the dog, 2the cat, 3the cat ,4the cat

-n选项用于禁止sed输出,但p会输出修改过的行,二者结合只输出修改过的行

示例4:

]# sed  's/cat/dog/w text2.txt' text1.txt 
1the dog, 2the cat, 3the cat ,4the cat
1the fox, 2the fox, 3the fox ,4the fox
]# cat text2.txt 
1the dog, 2the cat, 3the cat ,4the cat

w只会把匹配的行输出到指定文件

替换字符
像匹配内容和替换内容有特殊字符,例如斜线,替换需要使用转义字符进行转义,但是这样可能造成歧义,例如
sed 's/\/bin\/bash/\/bin/\csh/' /etc/passwd
解决方法:sed允许使用其他字符作为替换命令中的字符串分隔符
sed 's!/bin/bash!/bin/csh!' /etc/passwd
示例5:

]# sed -n 's!/bin/bash!/bin/sh!p' /etc/passwd
root:x:0:0:root:/root:/bin/sh
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/sh

使用地址
默认情况下,sed会作用于文本的所有行,如果只想作用于特定行,则必须使用行寻址(line addressing)
sed有两种行寻址型是:

以数字型是表示行区间
用文本模式来过滤出行

两种形式使用相同的格式来指定地址:

[address] command
或
address {
command1
command2
command3
}

1.以数字方式的行寻址

sed会将文本流中的第一行编号为1,然后按顺序为后续的行分配行号
按数字方式寻址有2种,包括:

1.按单个行号进行单行寻址
2.按区间进行区间范围寻址(区间格式:起始行号、逗号及结尾行号),尾行行号为$

示例6:

]# sed '2,$s/fox/cat/' data1.txt 
1the quick brown fox jumps over the lazy dog
2the quick brown cat jumps over the lazy dog
3the quick brown cat jumps over the lazy dog

2.使用文本模式过滤器

sed允许指定文本模式来过滤出命令要作用的行,格式
/pattern/command
示例7:

]# grep yurq /etc/passwd
yurq:x:1000:1000:yurq:/home/yurq:/bin/bash
]# sed -n '/yurq/s/bash/csh/p' /etc/passwd
yurq:x:1000:1000:yurq:/home/yurq:/bin/csh

3.组合命令

如果要在单行执行多个命令,可以用花括号将命令组合在一起,例如

]# sed '3,${
> s/brown/green/
> s/fox/elephant/
> }' data1.txt
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog
3the quick green elephant jumps over the lazy dog
4the quick green elephant jumps over the lazy dog
5the quick green elephant jumps over the lazy dog

4. 匹配模式

sed -i '/swap/s/^/#/' fstab

对包含’swap’的第一行,在行首加#

5. 删除行

删除(delete)命令d,会删除匹配指定寻址模式的所有行
删除单行:
sed '3d' data1.txt
删除区间
sed '2,3d' data1.txt
sed '2,$d' data1.txt

]# sed '2,3d' data1.txt 
1the	quick brown fox jumps over the lazy dog
4the quick brown fox jumps over the lazy dog
5the quick brown fox jumps over the lazy dog
]# sed '2,$d' data1.txt 
1the	quick brown fox jumps over the lazy dog

匹配模式同样适用

]# sed '/1the/d' data1.txt 
2the quick brown fox jumps over the lazy dog
3the quick brown fox jumps over the lazy dog

sed编辑器不会修改原文件,所以要保留修改后的结果可以使用重定向

区间模式匹配:使用两个文本模式来删除某个匹配区间内的行,这么作要小心,因为第一个模式会‘打开’删除功能,直到第二个模式匹配才会‘关闭’行删除功能,sed会删除两个指定行之间的所有行(包括指定行)

]# sed '/1/,/3/d' data1.txt
4the quick brown fox jumps over the lazy dog
5the quick brown fox jumps over the lazy dog

这样删除行有两个问题:
1,如果第二个模式未匹配,则会删除匹配到第一个模式的所有行
2,如果第一个模式在第二个模式后再次出现,则会继续删除,直到再次匹配到第二个模式

6. 插入和附加文本

插入(insert)命令i会在指定行前增加一个新行
附加(append)命令a会在指定行后增加一个新行
格式:sed '[address]command\new line'
例如:

]# echo "test line2"|sed 'i\test line1'
test line1
test line2
]# echo "test line2"|sed 'a\test line1'
test line2
test line1
]# echo "test line2"|sed 'a\
> test line1'
test line2
test line1

向数据流插入数据,格式
sed '[address]command\new line' file
示例:

]# sed '3i\===============' data1.txt 
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog
===============
3the quick brown fox jumps over the lazy dog
4the quick brown fox jumps over the lazy dog

向第三行插入

]# sed '1i\===============' data1.txt 
===============
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog

向文档首部插入

]# sed '$a\===============' data1.txt 
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog
3the quick brown fox jumps over the lazy dog
4the quick brown fox jumps over the lazy dog
5the quick brown fox jumps over the lazy dog
===============

向文档尾部追加

插入和追加同样支持文本模式寻址,例如:

]# sed '/quick brown/a\===============' data1.txt 
1the	quick brown fox jumps over the lazy dog
===============
2the quick brown fox jumps over the lazy dog
===============
3the quick brown fox jumps over the lazy dog
===============

7. 修改行

修改(change)命令c允许修改数据流中整行文本的内容,工作模式跟插入附加相同,必须指定新行
格式:sed '[address]command\newline' [file]
示例:

]# sed '1c\===============' data1.txt 
===============
2the quick brown fox jumps over the lazy dog
3the quick brown fox jumps over the lazy dog

文本模式寻址

]# cat text1.txt 
1the cat, 2the cat, 3the cat ,4the cat
1the fox, 2the fox, 3the fox ,4the fox
[root@localhost section19]# sed '/2the cat/c\===========' text1.txt 
===========
1the fox, 2the fox, 3the fox ,4the fox

使用区间,会替换区间内所有行内容为新行内容,而不是每行都替换

]# sed '2,3c\new line' data1.txt 
1the	quick brown fox jumps over the lazy dog
new line
4the quick brown fox jumps over the lazy dog
5the quick brown fox jumps over the lazy dog

8. 转换命令

转换(transform)命令y是唯一可以处理单个字符的sed命令,格式
[address]y/inchars/outchars/
转换命令会对inchars和outchars值进行一一映射,inchars第一个字符对应outchars第一个字符,inchars第二个字符对应outchars第二个字符,这个映射过程一直持续到处理完指定字符。如果inchars和outchars长度不同,sed会产生错误信息
转换命令是全局命令,会对文本行中所有指定字符自动进行转换,例如

]# sed 'y/1234/5678/' text1.txt 
5the cat, 6the cat, 7the cat ,8the cat
5the fox, 6the fox, 7the fox ,8the fox

9. 打印

有三个命令可以用来打印数据流中的信息:

1.p命令打印文本行(注意此处的p为命令,而前文中的flag p指的是替换标记)
2.等号(=)打印行号
3.l用来列出行

打印行

]# echo "test " |sed 'p'
test 
test 

打印两次是因为sed匹配后输出一次,p命令输出一次

]# echo "test " |sed ''
test 

组合命令

]# sed -n '/cat/{
p
s/cat/test/p
}' text1.txt
1the cat, 2the cat, 3the cat ,4the cat
1the test, 2the cat, 3the cat ,4the cat

打印行号

]# sed '=' text1.txt 
1
1the cat, 2the cat, 3the cat ,4the cat
2
1the fox, 2the fox, 3the fox ,4the fox

只显示匹配行

]# sed -n '/cat/{
=
p           
}' text1.txt
1
1the cat, 2the cat, 3the cat ,4the cat

10. 列出行

列出(list)命令l可以打印数据流中的文本和不可打印的ASCII字符(不可打印字符要么在其八进制值前加\,要么使用C风格的命名法),比如\t,代表制表符

]# sed -n 'l' data1.txt 
1the\tquick brown fox jumps over the lazy dog$

11. 使用sed处理文件

替换命令s包含一些可以用于文件的替换标记,sed还包含一些命令可以实现同样目标

12. 写入文件

写入(write)命令w用来向文件写入行,格式
[address]w filename
filename可以是绝对或相对路径,但sed的用户需要对此文件有写权限,支持任意类型寻址模式
示例:

]# sed '1,2w text1.txt' data1.txt 
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog
3the quick brown fox jumps over the lazy dog
4the quick brown fox jumps over the lazy dog
5the quick brown fox jumps over the lazy dog
[root@localhost section19]# cat text1.txt 
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog

可以使用-n关闭向STDOUT输出

]# sed -n '/yurq/w mypasswd.txt' /etc/passwd
]# cat mypasswd.txt 
yurq:x:1000:1000:yurq:/home/yurq:/bin/bash

13. 从文件读取数据

读取(read)命令r允许将一个独立文件中的数据插入到数据流中,插入到指定地址后,格式
[address]r filename
示例

]# sed '$r mypasswd.txt' text1.txt 
1the	quick brown fox jumps over the lazy dog
2the quick brown fox jumps over the lazy dog
yurq:x:1000:1000:yurq:/home/yurq:/bin/bash

14. sed用户实例

替换操作:s命令

替换文本中的字符串:

 sed 's/book/books/' file

-n选项和p命令一起使用表示只打印那些发生替换的行:

 sed -n 's/test/TEST/p' file

直接编辑文件选项-i,会匹配file文件中每一行的第一个book替换为books

 sed -i 's/book/books/g' file

全面替换标记g

使用后缀 /g 标记会替换每一行中的所有匹配:

 sed 's/book/books/g' file

当需要从第N处匹配开始替换时,可以使用 /Ng:

 echo sksksksksksk | sed 's/sk/SK/2g' 
 skSKSKSKSKSK
 echo sksksksksksk | sed 's/sk/SK/3g'
 skskSKSKSKSK  
 echo sksksksksksk | sed 's/sk/SK/4g'
 skskskSKSKSK 

定界符

以上命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符

 sed 's:test:TEXT:g' 
 sed 's|test|TEXT|g' 

定界符出现在样式内部时,需要进行转义:

 sed 's/\/bin/\/usr\/local\/bin/g'

删除操作:d命令

删除空白行:

 sed '/^$/d' file

删除文件的第2行:

 sed '2d' file

删除文件的第2行到末尾所有行:

 sed '2,$d' file

删除文件最后一行:

 sed '$d' file

删除文件中所有开头是test的行:

 sed '/^test/'d file

已匹配字符串标记&

正则表达式 \w+ 匹配每一个单词,使用 [&] 替换它,& 对应于之前所匹配到的单词:

 echo this is a test line | sed 's/\w\+/[&]/g'
 [this] [is] [a] [test] [line] 

所有以192.168.0.1开头的行都会被替换成它自已加localhost:

 sed 's/^192.168.0.1/&localhost/' file 192.168.0.1localhost

子串匹配标记\1

匹配给定样式的其中一部分:

 echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/' 
 this is 7 in a number

命令中 digit 7,被替换成了 7。样式匹配到的子串是 7,(…) 用于匹配子串,对于匹配到的第一个子串就标记为 \1,依此类推匹配到的第二个结果就是 \2,例如:

 echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/' 
 BBB aaa

love被标记为1,所有loveable会被替换成lovers,并打印出来:

 sed -n 's/\(love\)able/\1rs/p' file
```bash
### 组合多个表达式

 sed '表达式' | sed '表达式'  等价于:  
 sed '表达式; 表达式'

引用

sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号。
```bash
 test=hello 
 echo hello WORLD | sed "s/$test/HELLO" 
 HELLO WORLD

选定行的范围:,(逗号)

所有在模板test和check所确定的范围内的行都被打印:

 sed -n '/test/,/check/p' file

打印从第5行开始到第一个包含以test开始的行之间的所有行:

 sed -n '5,/^test/p' file

对于模板test和west之间的行,每行的末尾用字符串aaa bbb替换:

 sed '/test/,/west/s/$/aaa bbb/' file

多点编辑:e命令

-e选项允许在同一行里执行多条命令:

 sed -e '1,5d' -e 's/test/check/' file

上面sed表达式的第一条命令删除1至5行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

和 -e 等价的命令是 --expression:

 sed --expression='s/test/check/' --expression='/love/d' file

从文件读入:r命令

file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面:

 sed '/test/r file' filename

写入文件:w命令

在example中所有包含test的行都被写入file里:

 sed -n '/test/w file' example

追加(行下):a\命令

将 this is a test line 追加到 以test 开头的行后面:

 sed '/^test/a\this is a test line' file

在 test.conf 文件第2行之后插入 this is a test line:

 sed -i '2a\this is a test line' test.conf 

插入(行上):

i\命令 将 this is a test line 追加到以test开头的行前面:

 sed '/^test/i\this is a test line' file

在test.conf文件第5行之前插入this is a test line:

 sed -i '5i\this is a test line' test.conf

下一个:n命令

如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续:

 sed '/test/{ n; s/aa/bb/; }' file 

变形:y命令

把1~10行内所有abcde转变为大写,注意,正则表达式元字符不能使用这个命令:

 sed '1,10y/abcde/ABCDE/' file

退出:q命令

打印完第10行后,退出sed sed ‘10q’ file 保持和获取:h命令和G命令 在sed处理文件的时候,每一行都被保存在一个叫模式空间的临时缓冲区中,除非行被删除或者输出被取消,否则所有被处理的行都将打印在屏幕上。接着模式空间被清空,并存入新的一行等待处理。

 sed -e '/test/h' -e '$G' file 

在这个例子里,匹配test的行被找到后,将存入模式空间,h命令将其复制并存入一个称为保持缓存区的特殊缓冲区内。第二条语句的意思是,当到达最后一行后,G命令取出保持缓冲区的行,然后把它放回模式空间中,且追加到现在已经存在于模式空间中的行的末尾。在这个例子中就是追加到最后一行。简单来说,任何包含test的行都被复制并追加到该文件的末尾。

保持和互换:h命令和x命令

互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换:

 sed -e '/test/h' -e '/check/x' file 

脚本scriptfile

sed脚本是一个sed的命令清单,启动Sed时以-f选项引导脚本文件名。Sed对于脚本中输入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多个命令,要用分号分隔。以#开头的行为注释行,且不能跨行。

 sed [options] -f scriptfile file(s)

打印奇数行或偶数行

方法1:

 sed -n 'p;n' test.txt  #奇数行 sed -n 'n;p' test.txt  #偶数行 

方法2:

 sed -n '1~2p' test.txt  #奇数行 sed -n '2~2p' test.txt  #偶数行 

打印匹配字符串的下一行

 grep -A 1 SCC URFILE 
 sed -n '/SCC/{n;p}' URFILE 
 awk '/SCC/{getline; print}' URFILE
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值