linux三剑客之:sed

admin 2021年04月09日 1,332次浏览

sed是Linux中的一种文本流编辑器,可以将上一步操作的结果通过重定向的方式引入到sed中进行二次处理;也可以在不打开文件的情况下,对指定文件中匹配到指定条件的内容进行批量修改

1、作用

文本流行编辑器,不对源文件进行编辑,而是从指定文件中一次读取一行,经过处理后输出到屏幕,然后继续读取其他指定行进行处理

2、语法

sed [选项] [[n1[,n2]] 操作]

其中n1,n2表示选择操作的行数范围,例如需要对10到20行进行操作处理,则使用10,20[function]

3、常用选项

  • -n:只将被处理过的行显示到屏幕上
  • -e:直接在命令行模式下进行sed的操作编辑
  • -f :从指定文件中读取编辑脚本
  • -r: 支持使用扩展正则表达式
  • -i: 直接修改源文件,操作需谨慎

4、常用操作

  • a:在当前行的 后面 插入文本,支持使用\n实现多行插入

  • i :在当前行的 前面 插入文本,支持使用\n实现多行插入

  • c:可以将n1到n2之间行的内容替换为c后面跟的字符

  • d:删除行,后面不需要跟内容

  • p:显示指定行,通常配合-n使用

  • s:替换指定数据

  • 替换使用方法

    s/需要替换的字符/替换后的字符/[g/p/w]
    
    • 替换标记
      g:行内全局替换
      p:显示替换成功的行
      w 文件:将替换成功的结果保存至指定文件中

5、使用示例

  • 示例使用数据

    [root@localhost ~]# nl test.txt 
         1	"Open Source" is a good mechanism to develop programs.
         2	apple is my favorite food.
         3	Football game is not use feet only.
         4	this dress doesn't fit me.
         5	However, this dress is about $ 3183 dollars.
         6	GNU is free air not free beer.
         7	Her hair is very beauty.
         8	I can't finish the test.
         9	Oh! The soup taste good.
    
  • 使用示例

    # 显示指定行
    [root@localhost ~]# nl test.txt | sed -n 2,5p
         2	apple is my favorite food.
         3	Football game is not use feet only.
         4	this dress doesn't fit me.
         5	However, this dress is about $ 3183 dollars.
    
    # 在指定行后面添加内容
    [root@localhost ~]# nl test.txt | sed '2a---line_1---'
         1	"Open Source" is a good mechanism to develop programs.
         2	apple is my favorite food.
    ---line_1---
         3	Football game is not use feet only.
         4	this dress doesn't fit me.
    	 ....
    	 
    # 在指定行前面添加内容
    [root@localhost ~]# nl test.txt | sed '2i---line_1---'
         1	"Open Source" is a good mechanism to develop programs.
    ---line_1---
         2	apple is my favorite food.
         3	Football game is not use feet only.
         4	this dress doesn't fit me.
         ....
    # 在指定内容处添加数据
    [root@localhost ~]# sed '/apple is my favorite food./a\--- add line ---' test.txt
    	1 "Open Source" is a good mechanism to develop programs.
    	2 apple is my favorite food.
    	--- add line ---
    	3 Football game is not use feet only.
    	4 this dress doesn't fit me.
    	5 However, this dress is about $ 3183 dollars.
    	6 GNU is free air not free beer.
    	7 Her hair is very beauty.
    	8 I can't finish the test.
    	9 Oh! The soup taste good.
    
    # 删除指定行
    [root@localhost ~]# nl test.txt | sed '2,5d'
         1	"Open Source" is a good mechanism to develop programs.
         6	GNU is free air not free beer.
         7	Her hair is very beauty.
         8	I can't finish the test.
         9	Oh! The soup taste good.
         
    # 从指定行开始,删除后面所有行
    [root@localhost ~]# nl test.txt | sed '5,$d'
         1	"Open Source" is a good mechanism to develop programs.
         2	apple is my favorite food.
         3	Football game is not use feet only.
         4	this dress doesn't fit me.
    
    # 替换指定内容
    ## 将示例中单个is替换成IS,并且只打印被替换的行
    [root@localhost ~]# nl test.txt | sed -n 's/ is / IS /gp'
         1	"Open Source" IS a good mechanism to develop programs.
         2	apple IS my favorite food.
         3	Football game IS not use feet only.
         5	However, this dress IS about $ 3183 dollars.
         6	GNU IS free air not free beer.
         7	Her hair IS very beauty.
         
    ## 替换整行数据
    [root@localhost ~]# nl test.txt | sed '2,5c 2-5 line'
         1	"Open Source" is a good mechanism to develop programs.
    2-5 line
         6	GNU is free air not free beer.
         7	Her hair is very beauty.
         8	I can't finish the test.
         9	Oh! The soup taste good.
         
    # 在最后一行的前面添加内容
    [root@localhost tmp]# nl test.txt | sed '$i --- add line ---'
         1  "Open Source" is a good mechanism to develop programs.
         2  apple is my favorite food.
         3  Football game is not use feet only.
         4  this dress doesn't fit me.
         5  However, this dress is about $ 3183 dollars.
         6  GNU is free air not free beer.
         7  Her hair is very beauty.
         8  I can't finish the test.
    --- add line ---
         9  Oh! The soup taste good.
    
    # 在最后一行的后面添加内容
    [root@localhost tmp]# nl test.txt | sed '$a --- add line ---'
         1  "Open Source" is a good mechanism to develop programs.
         2  apple is my favorite food.
         3  Football game is not use feet only.
         4  this dress doesn't fit me.
         5  However, this dress is about $ 3183 dollars.
         6  GNU is free air not free beer.
         7  Her hair is very beauty.
         8  I can't finish the test.
         9  Oh! The soup taste good.
    --- add line ---
    
    # 删除空白行
    ## 由于可能存在特殊不可见字符,因此删除空白行可以使用以下方法
    [root@localhost tmp]#  sed '/^[[:space:]]*$/d' test.txt