DeTechn Blog

写一个 sed 命令,修改 /tmp/input.txt 文件的内容?

要求:

删除所有空行。
一行中,如果包含 “11111”,则在 “11111” 前面插入 “AAA”,在 “11111” 后面插入 “BBB” 。比如:将内容为 0000111112222 的一行改为 0000AAA11111BBB2222 。
[root@~]## cat -n /tmp/input.txt

 1  000011111222
 2
 3  000011111222222
 4  11111000000222
 5
 6
 7  111111111111122222222222
 8  2211111111
 9  112222222
10  1122
11

删除所有空行命令

[root@~]## sed '/^$/d' /tmp/input.txt
000011111222
000011111222222
11111000000222
111111111111122222222222
2211111111
112222222
1122

插入指定的字符

[root@~]## sed 's#(11111)#AAA\1BBB#g' /tmp/input.txt
0000AAA11111BBB222
0000AAA11111BBB222222
AAA11111BBB000000222
AAA11111BBBAAA11111BBB11122222222222
22AAA11111BBB111
112222222
1122

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »