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

 

0 前言

 在

Linux shell编程学习笔记73:sed命令——沧海横流任我行(上)-CSDN博客文章浏览阅读684次,点赞32次,收藏24次。在大数据时代,我们要面对大量数据,有时需要对数据进行替换、删除、新增、选取等特定工作。在Linux中提供很多数据处理命令,如果我们要以行为单位进行数据处理,可以使用sed命令。https://blog.csdn.net/Purpleendurer/article/details/141307421?spm=1001.2014.3001.5501中,我们研究了sed的基础知识。

Linux shell编程学习笔记74:sed命令——沧海横流任我行(中)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/141369789?spm=1001.2014.3001.5501中,我们见识了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 ~] 

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 ~] 

 

 

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 ~] 

 

 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 ~] 

 

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 ~] 

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 ~] 

 

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 ~] 

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 ~] 

 

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 ~] 

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值