首页 > 系统相关 >Linux之sed练习掌握

Linux之sed练习掌握

时间:2022-09-22 10:57:33浏览次数:53  
标签:root 练习 sed ecs Linux line data 76840553

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

相关文章

  • Linux安装jdk
    1.在官网下载jdk安装文件,下载地址:https://www.oracle.com/java/technologies/downloads/ 我一直使用的是8,所以我下载的是 jdk-8u341-linux-x64.tar.gz2.解压到/opt/......
  • Linux安装tomcat后启动报错Cannot find ./catalina.sh的解决方法
    Linux安装tomcat后启动报错:Cannotfind./catalina.shThefileisabsentordoesnothaveexecutepermissionThisfileisneededtorunthisprogram原因:无权限解......
  • linux 中 利用命令向文件的末尾添加空行
     001、(base)[root@PC1test]#seq3>a.txt(base)[root@PC1test]#cata.txt##测试数据123(base)[root@PC1test]#echo>>a.txt##末尾追加一......
  • Linux目录结构
    一、基本介绍linux文件系统采用级层的树状目录结构,最上层的就是根目录/,在此基础上创建其他目录。在linux中一切皆为文件二、详细目录介绍/bin(不可随意更改):该目录下......
  • linux网络服务-SSH服务
    1.哪些设置能提升SSH远程管理的安全等级?      ......
  • linux中basename命令
     001、basename命令可以获取末尾文件名和末尾目录名(base)[root@PC1home]#basenametest2/a.txta.txt(base)[root@PC1home]#basenametest2/dir01/dir01 ......
  • linux 中 date +%s 获取1970年以来的秒数
     001、(base)[root@PC1home]#date+%s1663810406(base)[root@PC1home]#date+%s1663810410 date+%s //从1970年1月1日00:00:00UTC到目前为......
  • linux系统管理类-资源查看
    1.linux中有许多系统资源需要监管,请问有哪些命令可以查看?cpu情况内存,硬盘情况centos6以下 使用ifconfig查询ip磁盘centos7 df磁盘空间查看 fdisk磁......
  • (No operations allowed after connection closed.). Possibly consider using a shor
    (Nooperationsallowedafterconnectionclosed.).PossiblyconsiderusingashortermaxLifetimevalue 先按提示添加maxLifetime的超时时间试试看spring:#......
  • linux系统管理类-备份策略
    1.如果一个系统没有任何的备份策略,请写出一个较为全面合理的备份方案!通用的系统数据目录-必须要备份的1.如果是数据库服务器:需要备份这些差异备份好处是,原始文件大......