linux sed 常用使用实例

写在前面的话

每篇一句

大巧若拙,大辩若讷。

sed 常用option

  • 啥参数不加,直接接文件。输出修改后的文件内容,并不对文件进行修改
  • -i: 直接修改原文件(建议备份源文件后操作)

在指定匹配行后添加行

源文件

1
2
3
4
[root@openfalon_zzq_host tmp]# cat test
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc

a 表示在之后添加

1
2
3
4
5
[root@openfalon_zzq_host tmp]# sed -i '/^bbbbbbbbbbb/a\after' test ;cat test
aaaaaaaaaaa
bbbbbbbbbbb
after
ccccccccccc

在指定匹配行前添加行

i 表示在之前添加

1
2
3
4
5
6
[root@openfalon_zzq_host tmp]# sed -i '/^bbbbbbbbbbb/i\before' test ;cat test
aaaaaaaaaaa
before
bbbbbbbbbbb
after
ccccccccccc

在指定行号添加行

N;后面只能使用偶数,且不可以为0

在第4行后添加

1
2
3
4
5
6
7
[root@openfalon_zzq_host tmp]# sed -i 'N;4a\after4' test ;cat test
aaaaaaaaaaa
before
bbbbbbbbbbb
after
after4
ccccccccccc

在第三行添加
i表示在当前行插入一行,如果指定行为4,其实最终的结果插入行的位置是第三行。

1
2
3
4
5
6
7
8
[root@openfalon_zzq_host tmp]# sed -i 'N;4i\before4' test ;cat test
aaaaaaaaaaa
before
before4
bbbbbbbbbbb
after
after4
ccccccccccc

全局替换

将before替换为now,假设需精确匹配则是sed -i ‘s/before$/now/g’ test

1
2
3
4
5
6
7
8
[root@openfalon_zzq_host tmp]# sed -i 's/before/now/g' test ;cat test
aaaaaaaaaaa
now
now4
bbbbbbbbbbb
after
after4
ccccccccccc

注释行

将以a字母开头的行添加注释#

1
2
3
4
5
6
7
8
[root@openfalon_zzq_host tmp]# sed -i 's/^a/#&/g' test ;cat test
#aaaaaaaaaaa
now
now4
bbbbbbbbbbb
#after
#after4
ccccccccccc

unix文件格式转dos

在DOS文件格式中使用CR/LF换行,在Unix下仅使用LF换行

1
2
3
4
5
6
7
8
9
10
11
[root@openfalon_zzq_host tmp]# sed 's/$/\r/' test > dos.txt
[root@openfalon_zzq_host tmp]# cat dos.txt
#aaaaaaaaaaa
now
now4
bbbbbbbbbbb
#after
#after4
ccccccccccc
[root@openfalon_zzq_host tmp]# file dos.txt
dos.txt: ASCII text, with CRLF line terminators

##dos 文件格式转unix

1
2
3
[root@openfalon_zzq_host tmp]# sed 's/.$//' dos.txt > unix.txt
[root@openfalon_zzq_host tmp]# file unix.txt
unix.txt: ASCII text

空格换成换行

源文件

1
2
[root@openfalon_zzq_host tmp]# cat test
1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.4 1.1.1.5

1
2
3
4
5
6
7
[root@openfalon_zzq_host tmp]# sed -i 's/ /\n/g' test
[root@openfalon_zzq_host tmp]# cat test
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5

每行行尾加

1
2
3
4
5
6
7
[root@openfalon_zzq_host tmp]# sed -i 's/$/&:22/g' test
[root@openfalon_zzq_host tmp]# cat test
1.1.1.1:22
1.1.1.2:22
1.1.1.3:22
1.1.1.4:22
1.1.1.5:22

每行行首加

1
2
3
4
5
6
7
[root@openfalon_zzq_host tmp]# sed -i 's/^/scp testfile root@&/g' test
[root@openfalon_zzq_host tmp]# cat test
scp testfile root@1.1.1.1:22
scp testfile root@1.1.1.2:22
scp testfile root@1.1.1.3:22
scp testfile root@1.1.1.4:22
scp testfile root@1.1.1.5:22

转义字符添加

1
2
3
4
5
6
7
[root@openfalon_zzq_host tmp]# sed -i 's/$/&\/tmp\/copyfile/g' test
[root@openfalon_zzq_host tmp]# cat test
scp testfile root@1.1.1.1:22/tmp/copyfile
scp testfile root@1.1.1.2:22/tmp/copyfile
scp testfile root@1.1.1.3:22/tmp/copyfile
scp testfile root@1.1.1.4:22/tmp/copyfile
scp testfile root@1.1.1.5:22/tmp/copyfile

删除配置文件中注释行和空格行,仅仅显示不修改源文件

1
2
3
4
5
6
7
8
9
10
11
[root@openfalon_zzq_host tmp]# cat test
#scp testfile root@1.1.1.1:22:22
scp testfile root@1.1.1.2:22:22
scp testfile root@1.1.1.3:22:22
scp testfile root@1.1.1.4:22:22
#scp testfile root@1.1.1.5:22:22
[root@openfalon_zzq_host tmp]# grep -v '^#' test |sed '/^[[:space:]]*$/d'
scp testfile root@1.1.1.2:22:22
scp testfile root@1.1.1.3:22:22
scp testfile root@1.1.1.4:22:22

删除行

删除空白行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@openfalon_zzq_host tmp]# cat test
#scp testfile root@1.1.1.1:22:22
scp testfile root@1.1.1.2:22:22
scp testfile root@1.1.1.3:22:22
scp testfile root@1.1.1.4:22:22
#scp testfile root@1.1.1.5:22:22
[root@openfalon_zzq_host tmp]# sed '/^$/d' test
#scp testfile root@1.1.1.1:22:22
scp testfile root@1.1.1.2:22:22
scp testfile root@1.1.1.3:22:22
scp testfile root@1.1.1.4:22:22
#scp testfile root@1.1.1.5:22:22

删除指定行
举例删除第5行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@openfalon_zzq_host tmp]# cat -n test
1 #scp testfile root@1.1.1.1:22:22
2 scp testfile root@1.1.1.2:22:22
3
4 scp testfile root@1.1.1.3:22:22
5 scp testfile root@1.1.1.4:22:22
6
7
8 #scp testfile root@1.1.1.5:22:22
[root@openfalon_zzq_host tmp]# sed '5d' test
#scp testfile root@1.1.1.1:22:22
scp testfile root@1.1.1.2:22:22
scp testfile root@1.1.1.3:22:22
#scp testfile root@1.1.1.5:22:22

删除N行开始到末尾所有行
N=2

1
2
3
4
5
6
7
8
9
10
11
[root@openfalon_zzq_host tmp]# cat -n test
1 #scp testfile root@1.1.1.1:22:22
2 scp testfile root@1.1.1.2:22:22
3
4 scp testfile root@1.1.1.3:22:22
5 scp testfile root@1.1.1.4:22:22
6
7
8 #scp testfile root@1.1.1.5:22:22
[root@openfalon_zzq_host tmp]# sed '2,$d' test
#scp testfile root@1.1.1.1:22:22

删除所有以scp 开头的行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@openfalon_zzq_host tmp]# cat -n test
1 #scp testfile root@1.1.1.1:22:22
2 scp testfile root@1.1.1.2:22:22
3
4 scp testfile root@1.1.1.3:22:22
5 scp testfile root@1.1.1.4:22:22
6
7
8 #scp testfile root@1.1.1.5:22:22
[root@openfalon_zzq_host tmp]# sed '/^scp/'d test
#scp testfile root@1.1.1.1:22:22
#scp testfile root@1.1.1.5:22:22

----纸上得来终觉浅绝知此事要躬行----
最好的赞赏是您的阅读!
0%