sed进阶

1 替换字符,正斜线(/2 sed 's!/bin/bash/!/bin/csh/!' /etc/passwd
3 ying:x:1000:1000::/home/ying:/bin/bash
4 laiying:x:1001:1002::/home/laiying:/bin/bash
 1 替换指定行的文本
 2 cat data1.txt 
 3 替换第2行的字符
 4 sed '2s/dog/cat/' data1.txt 
 5 The quick brown fox jumps over the lazy dog. 
 6 The quick brown fox jumps over the lazy cat. 
 7 The quick brown fox jumps over the lazy dog. 
 8 The quick brown fox jumps over the lazy dog.
 9 
10 替换第2行到第3行之间的文本
11 [root@localhost ~]# sed '2,3s/dog/cat/' data1.txt 
12 The quick brown fox jumps over the lazy dog. 
13 The quick brown fox jumps over the lazy cat. 
14 The quick brown fox jumps over the lazy cat. 
15 The quick brown fox jumps over the lazy dog.
16 
17 文本中从某行开始的所有行,可以用特殊地址——美元符。
18 [root@localhost ~]# sed '2,$s/dog/cat/' data1.txt 
19 The quick brown fox jumps over the lazy dog. 
20 The quick brown fox jumps over the lazy cat. 
21 The quick brown fox jumps over the lazy cat. 
22 The quick brown fox jumps over the lazy cat.
23  使用文本模式过滤器
24 修改用户Samantha的默认shell,
25 $ grep Samantha /etc/passwd 
26 Samantha:x:502:502::/home/Samantha:/bin/bash
27 $ sed '/Samantha/s/bash/csh/' /etc/passwd 
28 root:x:0:0:root:/root:/bin/bash 
29 bin:x:1:1:bin:/bin:/sbin/nologin 
30 [...] 
31 Christine:x:501:501:Christine B:/home/Christine:/bin/bash 
32 Samantha:x:502:502::/home/Samantha:/bin/csh 
33 Timothy:x:503:503::/home/Timothy:/bin/bash 
34 
35 命令组合,在单行上执行多条命令
36 [root@localhost ~]# sed '2{
37 > s/fox/elephant/
38 > s/dog/cat/
39 > }' data1.txt
40 The quick brown fox jumps over the lazy dog. 
41 The quick brown elephant jumps over the lazy cat. 
42 The quick brown fox jumps over the lazy dog. 
43 The quick brown fox jumps over the lazy dog.
删除行
 1 删除命令d,它会删除匹配指定寻址模式的所有行。使用该命令时要特别小心,如果你忘记加入寻址模式的话,流中的所有文本行都会被删除。
 2 [root@localhost ~]# cat data6.txt 
 3 This is line number 1. 
 4 This is line number 2. 
 5 This is line number 3. 
 6 This is line number 4.
 7 
 8 删除指定行,第3行
 9 [root@localhost ~]# sed '3d' data6.txt 
10 This is line number 1. 
11 This is line number 2. 
12 This is line number 4.
13 
14 删除指定区间的行,2-3行
15 [root@localhost ~]# sed '2,3d' data6.txt 
16 This is line number 1. 
17 This is line number 4.
18 
19 删除指定行到行尾的所有行 $
20 [root@localhost ~]# sed '3,$d' data6.txt 
21 This is line number 1. 
22 This is line number 2. 
23 
24 删掉包含匹配指定模式的行
25 [root@localhost ~]# sed '/number 1/d' data6.txt 
26 This is line number 2. 
27 This is line number 3. 
28 This is line number 4.

插入和附加文本
sed编辑器允许向数据流插入和附加文本行。
插入(insert)命令(i)会在指定行前增加一个新行;
附加(append)命令(a)会在指定行后增加一个新行。
它们不能在单个命令行上使用,必须指定是要将行插入还是附加到另一行

[root@localhost ~]# echo 'Test Line2' | sed 'iTest Line 1'
Test Line 1
Test Line2
[root@localhost ~]# echo "Test Line 2" | sed 'aTest Line 1'
Test Line 2
Test Line 1

将数据插入到指定行前,插入到第三行前
[root@localhost ~]# sed '3i
> This is an inserted line' data6.txt
This is line number 1. 
This is line number 2. 
This is an inserted line
This is line number 3. 
This is line number 4.

将数据插入到指定行后,插入到第三行后
[root@localhost ~]# sed '3a
> This is an appended line' data6.txt
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is an appended line
This is line number 4.

将新行附加到末尾
[root@localhost ~]# sed '$a
> This is a new line of text' data6.txt
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is line number 4.
This is a new line of text
将新数据插入到首行
[root@localhost ~]# sed '1i
> this is one line fo new text' data6.txt
this is one line fo new text
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is line number 4.
修改行
修改(change)命令允许修改数据流中整行文本的内容。它跟插入和附加命令的工作机制
一样,你必须在sed命令中单独指定新行。
 1 [root@localhost ~]# cat data6.txt 
 2 This is line number 1. 
 3 This is line number 2. 
 4 This is line number 3. 
 5 This is line number 4.
 6 
 7 修改第三行中的文本
 8 [root@localhost ~]# sed '3c
 9 > This is a chnged line of tex' data6.txt
10 This is line number 1. 
11 This is line number 2. 
12 This is a chnged line of tex
13 This is line number 4.
1 也可以使用文本模式修改命令会修改它匹配的数据流中的任意文本行
2 [root@localhost ~]# sed '/number 2/c
3 > this is a changed line of text' data6.txt
4 This is line number 1. 
5 this is a changed line of text
6 This is line number 3. 
7 This is line number 4.
转换命令,转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令,转换命令会对inchars和outchars值进行一对一的映射。inchars中的第一个字符会被转换为outchars中的第一个字符,第二个字符会被转换成outchars中的第二个字符。
[root@localhost ~]# sed 'y/123/789/' data6.txt 
This is line number 7. 
This is line number 8. 
This is line number 9. 
This is line number 4.
这个映射过程会一直持续到处理完指定字符。如果inchars和outchars的长度不同,则sed编辑器会产生一条错误消息。
[root@localhost ~]# sed 'y/number/Num/' data6.txt 
sed:-e 表达式 #1,字符 13:“y”命令的字符串长度不同
打印
-n加p打印包含匹配文本模式的行
[root@localhost ~]# sed -n '2,3p' data6.txt 
This is line number 2. 
This is line number 3.

如果需要在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行。
#修改第三行的文本并打印未修改和已修改后的文本
[root@localhost ~]# sed -n '/3/{
> p
> s/line/test/p
> }' data6.txt

打印行号  =
[root@localhost ~]# sed '=' data6.txt 
1
This is line number 1. 
2
This is line number 2. 
3
This is line number 3. 
4
This is line number 4.
在文本中打印指定行
[root@localhost ~]# sed -n '/number 4/{
> =
> p
> }' data6.txt
4
This is line number 4.
sed写入文件
w命令用来向文件写入行。该命令的格式如下:
[address]w filename

#将data6里面1,2行写入到test文件中
sed '1,2w test.txt' data6.txt 
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is line number 4.
 cat test.txt 
This is line number 1. 
This is line number 2.

如果你不想让行显示到STDOUT上,你可以用sed命令的-n选项
将包含文本模式的数据行写入目标文件。
[root@localhost ~]# cat data11.txt 
Blum, R Browncoat 
McGuiness, A Alliance 
Bresnahan, C Browncoat 
Harken, C Alliance

[root@localhost ~]# sed -n '/Browncoat/w Browncoats.txt' data11.txt 
[root@localhost ~]# cat Browncoats.txt 
Blum, R Browncoat 
Bresnahan, C Browncoat 

从文件中读取数据
cat data12.txt 
This is an added line. 
This is the second added line.
#将data12里面的数据从dada5的第三行里面插入
$ sed '3r data12.txt' data6.txt 
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is an added line. 
This is the second added line. 
This is line number 4.
#使用文本模式地址效果也一样
$ sed '/number 2/r data12.txt' data6.txt 
This is line number 1. 
This is line number 2. 
This is an added line. 
This is the second added line. 
This is line number 3. 
This is line number 4.

末尾添加文本,只需用美元符地址符就行了
$ sed '$r data12.txt' data6.txt 
This is line number 1. 
This is line number 2. 
This is line number 3. 
This is line number 4. 
This is an added line. 
This is the second added line.
也可以r读取命令和d删除命令一起使用
#将notice文本中的LIST替换成data11里面的文本
[root@localhost ~]# cat notice.std 
Would the following people: 
LIST 
please report to the ship's captain.

[root@localhost ~]# sed '/LIST/{
r data11.txt
d
}' notice.std
Would the following people: 
Blum, R Browncoat 
McGuiness, A Alliance 
Bresnahan, C Browncoat 
Harken, C Alliance
please report to the ship's captain.
原文地址:https://www.cnblogs.com/YingLai/p/12143190.html