美文网首页
Sed命令详解+如何替换换行符

Sed命令详解+如何替换换行符

作者: 夜空最亮的9星 | 来源:发表于2018-08-08 22:07 被阅读6次

基础用法详解

(1)第一行之后添加一行

[root@localhost ~]# nl file.txt | sed "1a add text"
     1  wtmp begins Mon Feb 24 14:26:08 2014
add text
     2  192.168.0.1
     3  162.12.0.123
     4  this is the last line

(2)第一行之前添加一行

[root@localhost ~]# nl file.txt | sed "1i add text"
add text
     1  wtmp begins Mon Feb 24 14:26:08 2014
     2  192.168.0.1
     3  162.12.0.123
     4  this is the last line

(3)删除第2,3行

[root@localhost ~]# nl file.txt | sed "2,3d"
     1  wtmp begins Mon Feb 24 14:26:08 2014
     4  this is the last line

(4)打印第2,3行

[root@localhost ~]# sed -n "2,3p" file.txt 
192.168.0.1
162.12.0.123

这里要提到的是,尽量使用-n,不然会出现这样的结果

[root@localhost ~]# sed "2,3p" file.txt 
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
192.168.0.1
162.12.0.123
162.12.0.123
this is the last line

(5)把168换成169
先看源文件

[root@localhost ~]# cat file.txt 
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
this is the last line

处理后

[root@localhost ~]# sed "s/168/169/g" file.txt 
wtmp begins Mon Feb 24 14:26:08 2014
192.169.0.1
162.12.0.123
this is the last line

(6)插入多行

[root@localhost ~]# nl file.txt | sed "2afirst\nsecond" file.txt 
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
first
second
162.12.0.123
this is the last line

(7)匹配数据,然后进行操作
只需要在上述的基础上加上正则匹配

sed "/匹配的模式/处理的方式" file.txt

sed "/^root/d" file.txt 对开始有root的删除
例如
匹配begin,并删除改行

[root@localhost ~]# nl file.txt | sed "/begin/d"
     2  192.168.0.1
     3  162.12.0.123
     4  this is the last line

匹配123,并且把含有123的行162都替换成172

[root@localhost ~]# nl file.txt | sed "/123/{s/162/172/g;q}"
     1  wtmp begins Mon Feb 24 14:26:08 2014
     2  192.168.0.1
     3  172.12.0.123
     4  this is the last line

这里大括号{}里可以执行多个命令,用;隔开即可,q是退出
(8)连续编辑 -e
删除第二行,并且匹配把last替换成new

     1  wtmp begins Mon Feb 24 14:26:08 2014
     3  162.12.0.123
     4  this is the new line

(9)直接修改文件,切记不要修改系统文件

[root@localhost ~]# sed -i "/begin/{s/24/25/g}" file.txt 
[root@localhost ~]# cat file.txt 
wtmp begins Mon Feb 25 14:26:08 2014
192.168.0.1
162.12.0.123
this is the last line

一个比较有趣的例子

如何替换\n也就是把所有的行都归为一行
第一种方式

sed ':a;N;$!ba;s/\n/ /g' file.txt

[root@localhost ~]# sed ':a;N;$!ba;s/\n/ /g' file.txt 
wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line

第二种方式

tr "\n" " " < file.txt

[root@localhost ~]# tr "\n" " " < file.txt 
wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line last linen

相关文章

  • [2020春假]Linux下的文本操作(sed篇)

    Chapter4 sed替换命令详解 sed的替换命令是最常用的,也是讲解最多的。sed的模式空间 sed的基本工...

  • Sed命令详解+如何替换换行符

    基础用法详解 (1)第一行之后添加一行 (2)第一行之前添加一行 (3)删除第2,3行 (4)打印第2,3行 这里...

  • Linux命令之sed批量替换字符串操作

    Linux中sed命令功能强大,本文将详细介绍如何使用sed命令进行字符串替换。一、基本的替换命令格式1:sed ...

  • 2019-11-15 linux的替换:sed -i "s/查找

    sed -i "s/查找字段/替换字段/g" 例如:test.txt包括: a;bc;d 使用命令把;替换成换行符...

  • tr与sed命令:将换行符替换为空格

    处理文本时需要将换行符替换为空格,若使用sed命令会比较麻烦,而使用tr命令非常方便。 输入文本(country....

  • 临时随笔sed/awk

    sed替换指定行内容 sed 命令行格式 脚本格式 基本操作命令 行定位 操作命令 实例 替换命令 高级操作命令 ...

  • 【linux命令之sed】

    sed的选项、命令、替换标记 命令格式 sed [options] 'command' file(s)sed [o...

  • sed命令学习

    sed命令详解 假设文件t1.txt内容为 常用组合 替换并输出: 读取test.txt并输出控制台,其中替换所有...

  • sed常用操作命令

    sed: stream editor , 流/行 编辑器 ; sed 命令详解: sed [OPTIONS].....

  • Linux-sed-1

    #############20190820- sed命令用法详解 sed命令用法 sed是一种流编辑器,它是文本处...

网友评论

      本文标题:Sed命令详解+如何替换换行符

      本文链接:https://www.haomeiwen.com/subject/uxdfbftx.html