深圳品牌网站制作报价,wordpress技术分析,什么是空壳网站,长春做网站费用0 前言 在
Linux shell编程学习笔记73#xff1a;sed命令——沧海横流任我行#xff08;上#xff09;-CSDN博客文章浏览阅读684次#xff0c;点赞32次#xff0c;收藏24次。在大数据时代#xff0c;我们要面对大量数据#xff0c;有时需要对数据进行替换、删除、新增、… 0 前言 在
Linux shell编程学习笔记73sed命令——沧海横流任我行上-CSDN博客文章浏览阅读684次点赞32次收藏24次。在大数据时代我们要面对大量数据有时需要对数据进行替换、删除、新增、选取等特定工作。在Linux中提供很多数据处理命令如果我们要以行为单位进行数据处理可以使用sed命令。https://blog.csdn.net/Purpleendurer/article/details/141307421?spm1001.2014.3001.5501中我们研究了sed的基础知识。
在
Linux shell编程学习笔记74sed命令——沧海横流任我行中-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/141369789?spm1001.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 -n1 12 23 34 45 56 67 7
[purpleendurer bash ~] seq 7 | cat -n | sed 3,4i\abc1 12 2
abc3 3
abc4 45 56 67 7
[purpleendurer bash ~] 1.1.1.2 在开头前插入abc
命令为 sed -e 1i\abc 其中sed命令的参数说明如下 -e解释脚本 1 指定第1行 i在前插入命令 \abc要插入的字符串 [purpleendurer bash ~] seq 7 | cat -n1 12 23 34 45 56 67 7
[purpleendurer bash ~] seq 7 | cat -n | sed -e 1i\abc
abc1 12 23 34 45 56 67 7
[purpleendurer bash ~] 1.1.1.3 在所有包含1的行前插入abc
命令为 sed /1*/i\abc [purpleendurer bash ~] seq 11 | cat -n 1 12 23 34 45 56 67 78 89 910 1011 11
[purpleendurer bash ~] seq 11 | cat -n | sed /1*/i\abc
abc1 1
abc2 2
abc3 3
abc4 4
abc5 5
abc6 6
abc7 7
abc8 8
abc9 9
abc10 10
abc11 11
[purpleendurer bash ~] 1.1.2 追加后插
我们可以使用a命令append来完成追加后插。
1.1.2.1在第3行及其后2行后追加abc
命令为 sed 3,2a\abc [purpleendurer bash ~] seq 7 | cat -n1 12 23 34 45 56 67 7
[purpleendurer bash ~] seq 7 | cat -n | sed 3,2a\abc1 12 23 3
abc4 4
abc5 5
abc6 67 7
[purpleendurer bash ~] 1.1.2.2 在末尾追加abc
命令为 sed $a\abc [purpleendurer bash ~] seq 7 | cat -n1 12 23 34 45 56 67 7
[purpleendurer bash ~] seq 7 | cat -n | sed $a\abc1 12 23 34 45 56 67 7
abc
[purpleendurer bash ~] 1.1.2.3 在偶数行后追加2行信息第1行是abc第2行是def
命令为 sed 0~2a\abc\ def [purpleendurer bash ~] seq 5 | cat -n1 12 23 34 45 5
[purpleendurer bash ~] seq 5 | cat -n | sed 0~2a\abc\
def1 12 2
abc
def3 34 4
abc
def5 5
[purpleendurer bash ~] 1.1.2.4 在所有包含1的行后面追加abc
命令为 sed /1/aabc [purpleendurer bash ~] seq 10 | cat -n1 12 23 34 45 56 67 78 89 910 10
[purpleendurer bash ~] seq 10 | cat -n | sed /1/aabc1 1
abc2 23 34 45 56 67 78 89 910 10
abc
[purpleendurer bash ~] 1.1.2.5 在只含有1个1的行后面追加abc
命令为 sed /1$/aabc [purpleendurer bash ~] seq 10 | cat -n1 12 23 34 45 56 67 78 89 910 10
[purpleendurer bash ~] seq 10 | cat -n | sed /1$/aabc1 1
abc2 23 34 45 56 67 78 89 910 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 ~]