1。操作文本 的内容。 cat sedtest.txt
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line.
2.sed数据添加。a:append
a-追加:向匹配行后面插入内容。i-插入:向匹配行前插入内容。这里之所以要同时介绍这 2 个脚本命令,因为它们的基本格式完全相同,如下所示:
[address]a(或 i)\新文本内容
2.1 a和i命令插入单行数据实例:
[root@ecs-76840553 sed]# cat sedtest.txt | sed '3i\insert' This is the header line. This is the first data line. insert This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '3a\insert' This is the header line. This is the first data line. This is the second data line. insert This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
2.2 将多行数据添加到数据流中,只需对要插入或附加的文本中的每一行末尾(除最后一行)添加反斜线\即可,例如:
[root@ecs-76840553 sed]# sed '3i\ > insert\ > insert2\ > insert3 > ' sedtest.txt This is the header line. This is the first data line. insert insert2 insert3 This is the second data line. This is the third data line. This is the fouth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.sed数据删除
d-删除:删除匹配的内容
如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。但使用该命令时要特别小心,如果你忘记指定具体行的话,文件中的所有内容都会被删除
命令格式:
[address]d
3.1 删除单行,指定地址
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3d' sedtest.txt This is the header line. This is the first data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.2 删除多行,指定区间。
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '2,3d' sedtest.txt This is the header line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
3.3 匹配文本删除
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/d' sedtest.txt This is the header line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
使用两个文本模式来删除某个区间内的行,但这么做时要小心,你指定的第一个模式会“打开”行删除功能,第二个模式会“关闭”行删除功能,因此,sed 会删除两个指定行之间的所有行(包括指定的行)
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/,/fourth/d' sedtest.txt This is the header line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
或者通过特殊的文件结尾字符,比如删除 data6.txt 文件内容中第 3 行开始的所有的内容:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3,$d' sedtest.txt This is the header line. This is the first data line. [root@ecs-76840553 sed]#
4.sed数据更改
整行数据更改:
c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。该命令的基本格式为:
[address]c\用于替换的新文本
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '3c\change line' sedtest.txt This is the header line. This is the first data line. change line This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# sed '/first/c\change line' sedtest.txt This is the header line. change line This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
单个字符替换:
y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:
[address]y/inchars/outchars/
转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。举个简单例子:
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt |sed 'y/line/ABCD/' ThBs Bs thD hDadDr ABCD. ThBs Bs thD fBrst data ABCD. ThBs Bs thD sDcoCd data ABCD. ThBs Bs thD thBrd data ABCD. ThBs Bs thD fourth data ABCD. ThBs Bs thD fBfth data ABCD. ThBs Bs thD sBxth data ABCD. ThBs Bs thD Aast ABCD. [root@ecs-76840553 sed]#
可以看到,inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相同位置的那个字符。 转换命令是一个全局命令,也就是说,它会将文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置,我们无法限定只转换在特定地方出现的字符。
字符串替换:
s-替换:替换掉匹配的内容,配合g使用,g代表全局
[root@ecs-76840553 sed]# cat sedtest.txt This is the header line. This is the first data line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed 's/line/change line/g' This is the header change line. This is the first data change line. This is the second data change line. This is the third data change line. This is the fourth data change line. This is the fifth data change line. This is the sixth data change line. This is the last change line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '2s/line/change line/g' This is the header line. This is the first data change line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]# cat sedtest.txt | sed '/first/s/line/change line/g' This is the header line. This is the first data change line. This is the second data line. This is the third data line. This is the fourth data line. This is the fifth data line. This is the sixth data line. This is the last line. [root@ecs-76840553 sed]#
标签:root,练习,sed,ecs,Linux,line,data,76840553 From: https://www.cnblogs.com/joyware/p/16718409.html