文本三剑客之sed

1. sed简介

全称: Stream EDitor

  • sed 是一种非交互式编辑器(即用户不必参与编辑过程),它使用预先设定好的编辑指令对输入的文本进行编辑,完成之后再输出编辑结构。
  • sed 基本上就是在做正则表达式匹配。
  • sed 命令是利用脚本来处理文本文件。
  • sed 命令可依照脚本的指令来处理、编辑文本文件。
  • sed 命令主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
  • sed 逐行处理文本,会读取整个文件。

2. 语法

sed [options] [script command] filenames
常用的 [options]
-n :或--quiet或--silent ,仅显示script处理后的结果。
-e :以选项中指定的script来处理输入的文本文件。即直接在命令列模式上进行 sed 的动作编辑。
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作。
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改文件内容,而不是输出到终端。
-i.bak: 修改文件内容前进行备份

动作说明:[n1,n2]function
n1, n2 :不见得会存在,一般代表选择进行动作的行数。例如,10,20[function],表示在10到20进行某一个动作。

常用的 [function]
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

正常情况下,sed命令所做的处理只是把操作结果(包括打印、删除等)输出到当前终端屏幕,并不会对原文件做任何更改。若希望直接修改文件内容,应添加选项 -i 。为了避免生产环境中因误操作导致系统故障,使用时要谨慎。

3. 示例

我们在实际操作中使用最多的命令。

  • 某些行加注释
  • 某些行取消注释
  • 删除某些行或者某些字符
  • 替换某些行或者某些字符
  • 在指定字符后面加内容
  • 在指定行下面加内容

3.1 给文件加注释

//现有以下文件
[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//给每一行加上注释。执行以下命令,只会将处理结果输出到终端,实际上文件并没有发生改变
[root@localhost ~]# sed  's/^/#/g' 123.txt 
#123
#456!
#
#123456789
#
#hubei!
#wuhan!
#hongshan!
#
#666,
#6,
#666666666,
#
#
#88.
#88888.
#88.
[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//加上-i选项就会对源文件进行修改
//
sed -i.bak 's/^/#/g' 123.txt //修改之前进行备份
[root@localhost ~]# sed -i 's/^/#/g' 123.txt 
[root@localhost ~]# cat 123.txt 
#123
#456!
#
#123456789
#
#hubei!
#wuhan!
#hongshan!
#
#666,
#6,
#666666666,
#
#
#88.
#88888.
#88.

//取消注释
[root@localhost ~]# sed -i 's/#//g' 123.txt 
[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//给某些行加注释
//以6开头的加注释,&的意思是匹配任意字符
[root@localhost ~]# sed  's/^6/#&/' 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

#666,
#6,
#666666666,


88.
88888.
88.
//1,3行加注释
[root@localhost ~]# sed  '1,3s/^/#/' 123.txt 
#123
#456!
#
123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

3.2 删除

[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//删除空白行
[root@localhost ~]# sed '/^$/d' 123.txt 
123
456!
123456789
hubei!
wuhan!
hongshan!
666,
6,
666666666,
88.
88888.
88.
//删除指定行
[root@localhost ~]# sed '1,2d' 123.txt 

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.
[root@localhost ~]# sed '4d' 123.txt 
123
456!


hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//删除带点的行,注意,这里的点需要转义
[root@localhost ~]# sed '/\./d' 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,

3.3 替换

[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//每一行匹配到的6替换成0
[root@localhost ~]# sed 's/6/0/' 123.txt 
123
450!

123450789

hubei!
wuhan!
hongshan!

066,
0,
066666666,


88.
88888.
88.
//加上-g表示全局替换,文中所有的6都替换成0
[root@localhost ~]# sed 's/6/0/g' 123.txt 
123
450!

123450789

hubei!
wuhan!
hongshan!

000,
0,
000000000,


88.
88888.
88.

3.4 增加

[root@localhost ~]# cat 123.txt 
123
456!

123456789

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//在123456789下面插入hello world
[root@localhost ~]# sed '/123456789/a\hello\ world' 123.txt 
123
456!

123456789
hello world

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.
//或者先确定它的行号,然后增加
[root@localhost ~]# sed '4a hello world' 123.txt 
123
456!

123456789
hello world

hubei!
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

//在hubei!后面增加666
[root@localhost ~]# sed 's/hubei!/hubei! 666/' 123.txt 
123
456!

123456789

hubei! 666
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.
//或者直接使用命令c进行改变
[root@localhost ~]# sed '6chubei! 666' 123.txt 
123
456!

123456789

hubei! 666
wuhan!
hongshan!

666,
6,
666666666,


88.
88888.
88.

3.5 查找

# 第三行
[root@lxr ~]# sed -n '3p' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin

# 第一行到第三行
[root@lxr ~]# sed -n '1,3p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

# 过滤包含root的行
[root@lxr ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

# sed -n /从哪里开始/,/到哪里结束/p;适用于取某个时间段内的日志
[root@lxr ~]# sed -n '/adm/,/mail/p' /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值