Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_shell编程

 

0 前言

 在

Linux shell编程学习笔记74:sed命令——沧海横流任我行(中)-博客中,我们见识了sed删除和替换功能的威力。

现在我们通过一些实例来见识一下sed插入等功能的威力。

1 sed实列

1.1 插入行

1.1.1 前插

我们可以使用i命令(insert)来完成前插。 

1.1.1.1 在第3行、第4行前插入'abc'

命令为:

sed '3,4i\abc'

其中sed命令的参数说明如下:

3,4 :指定第3行、第4行

i:在前插入命令

\abc:要插入的字符串

[purpleendurer @ bash ~]  seq 7 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '3,4i\abc'
     1  1
     2  2
abc
     3  3
abc
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_linux_02

1.1.1.2 在开头前插入'abc'

命令为:

sed -e '1i\abc'

其中sed命令的参数说明如下:

-e:解释脚本

1 :指定第1行

i:在前插入命令

\abc:要插入的字符串

[purpleendurer @ bash ~]  seq 7 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed -e '1i\abc'
abc
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]

 

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_学习笔记_03

 

1.1.1.3 在所有包含1的行前插入'abc' 

命令为:

sed  '/1*/i\abc'

[purpleendurer @ bash ~] seq 11 | cat -n 
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
     8  8
     9  9
    10  10
    11  11
[purpleendurer @ bash ~] seq 11 | cat -n | sed  '/1*/i\abc'
abc
     1  1
abc
     2  2
abc
     3  3
abc
     4  4
abc
     5  5
abc
     6  6
abc
     7  7
abc
     8  8
abc
     9  9
abc
    10  10
abc
    11  11
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_linux_04

 

 1.1.2 追加(后插)

我们可以使用a命令(append)来完成追加(后插)。 

1.1.2.1在第3行及其后2行后追加'abc'

命令为:

sed '3,+2a\abc'

[purpleendurer @ bash ~]  seq 7 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '3,+2a\abc'
     1  1
     2  2
     3  3
abc
     4  4
abc
     5  5
abc
     6  6
     7  7
[purpleendurer @ bash ~]

 

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_学习笔记_05

1.1.2.2 在末尾追加'abc'

命令为:

sed '$a\abc'

[purpleendurer @ bash ~]  seq 7 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '$a\abc'
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
abc
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_shell编程_06

1.1.2.3 在偶数行后追加2行信息,第1行是abc,第2行是def

命令为:

sed  '0~2a\abc\
def'

[purpleendurer @ bash ~] seq 5 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
[purpleendurer @ bash ~] seq 5 | cat -n | sed  '0~2a\abc\
def'
     1  1
     2  2
abc
def
     3  3
     4  4
abc
def
     5  5
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_shell编程_07

 

1.1.2.4 在所有包含1的行后面追加abc

命令为:

sed '/1/aabc'

 

[purpleendurer @ bash ~] seq 10 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
     8  8
     9  9
    10  10
[purpleendurer @ bash ~] seq 10 | cat -n | sed '/1/aabc'
     1  1
abc
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
     8  8
     9  9
    10  10
abc
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_脚本编程_08

1.1.2.5 在只含有1个1的行后面追加abc 

命令为:

sed '/1$/aabc'

[purpleendurer @ bash ~] seq 10 | cat -n
     1  1
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
     8  8
     9  9
    10  10
[purpleendurer @ bash ~] seq 10 | cat -n | sed '/1$/aabc'
     1  1
abc
     2  2
     3  3
     4  4
     5  5
     6  6
     7  7
     8  8
     9  9
    10  10
[purpleendurer @ bash ~]

 

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_shell编程_09

1.2 打印行号

我们可以使用等号(=)来代表行号。

命令为:

sed

 

[purpleendurer @ bash ~] echo "aaa" >  t.txt
[purpleendurer @ bash ~] echo "bbb" >> t.txt
[purpleendurer @ bash ~] echo "ccc" >> t.txt
[purpleendurer @ bash ~] cat t.txt
aaa
bbb
ccc
[purpleendurer @ bash ~] sed '=' t.txt
1
aaa
2
bbb
3
ccc
[purpleendurer @ bash ~]

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)_学习笔记_10