文本三剑客:都是按行读取后处理。
- grep 过滤行内容。
- awk 过滤字段。
- sed 过滤行内容;修改行内容。
sed
sed概述
sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。 sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。 sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
sed工作流程
sed 的工作流程主要包括读取、执行和显示三个过程: 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。 执行:默认情况下,所有的sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed 命令 将会在所有的行上依次执行。 显示font>:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。
sed基本用法
arduino复制代码sed -e '操作' 文件1 文件2
sed -n -e '操作' 文件1 文件2
sed -f 脚本文件 文件1 文件2
sed -i -e '操作' 文件1 文件2
sed常用选项
| 选项 | 作用 |
|---|---|
| -e 或 - -expression= | 多点编辑 |
| -f 或- -file= | 表示用指定的脚本文件来处理输入的文本文件。 |
| -h 或- -help | 显示帮助。 |
| -n | 不输出模式空间内容到屏幕,即不自动打印,加p,又恢复自动打印 |
| -i | 备份文件文件并原处编辑 |
| -r | 使用扩展正则表达式 |
sed命令的常用操作
| 操作符 | 作用 |
|---|---|
| s | 替换,替换指定字符。 |
| d | 删除,删除选定的行。 |
| a | 增加,在当前行下面增加一行指定内容。 |
| i | 插入,在选定行上面插入一行指定内容。 |
| c | 替换,将选定行替换为指定内容。 |
| Y | 字符转换,转换前后的字符长度必须相同。 |
| p | 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容; 如果有非打印字符,则以 AscII码输出。其通常与_n"选项一起使用。 |
| = | 打印行号。 |
| l(小写L) | 打印数据流中的文本和不可打印的ASCII字符(比如结束符s、制表符\t) |
sed的使用
打印
- sed ‘ ’交互模式输入一行,自动打印一行相同的
- sed -n ‘ ’ 交互模式输入一行,关闭自动打印
- sed -n ‘ p’加上p又恢复自动打印
[root@z1 opt]#sed ' '
11
11
22
22
33
33
44
44
[root@z1 opt]#sed -n ' '
11
22
33
[root@z1 opt]#sed -n ' p'
11
11
22
22
33
33
查看文件,打印所有内容
方法1
sed ' ' /etc/fstab
方法2
sed -n ' p' /etc/fstab
打印指定行内容
#例如:打印第四行内容
[root@z1 opt]#ifconfig ens33|sed -n '2p'
inet 192.168.254.100 netmask 255.255.255.0 broadcast 192.168.254.255
打印指定多行内容
#打印2行加4行,就是打印第二行到第6行
[root@z1 opt]#cat -n /etc/passwd|sed -n '2,+4p'
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
打印多行后退出
[root@z1 opt]#sed '3q' /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@z1 opt]#sed -n '$p' /etc/passwd
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
正则表达式表示
#查找基本格式:
sed -n '//,//p' 文件名
第一个//表示开头位置,第二个//表示结尾位置
过滤关键字
[root@z1 opt]#sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
从第#行开始匹配
sed -n '2,/root/p' /etc/passwd
找到第#个关键字为止
sed -n '/root/,3p' /etc/passwd
删除
删除指定行
#删除指定行并不是真正删除,只是将删除了的结果显示出来,并不是真正删除了
[root@z1 opt]#cat seq.txt
1
2
3
4
5
6
7
8
9
10
[root@z1 opt]#cat seq.txt |sed '3d'
1
2
4
5
6
7
8
9
10
[root@z1 opt]#cat seq.txt
1
2
3
4
5
6
7
8
9
10
删除指定的多行
[root@z1 opt]#sed '2,4d' seq.txt
1
5
6
7
8
9
10
删除文本中的空行
[root@z1 ~]#sed '/^$/d' /opt/seq.txt
1
2
3
4
5
6
7
8
9
10
删除以指定字符结尾的行及取反
sed '/nologin$/!d' /etc/passwd
插入
在指定行后插入
[root@z1 opt]#cat seq.txt
1
2
4
5
6
7
8
9
10
[root@z1 opt]#sed -i '2a hehe' seq.txt
[root@z1 opt]#cat seq.txt
1
2
hehe
4
5
6
7
8
9
10
*插入空行,修改文件的换行,要多加一个*
[root@z1 opt]#cat seq.txt
1
2
hehe
4
5
6
7
8
9
10
[root@z1 opt]#sed -i '2a\\n hehe' seq.txt
[root@z1 opt]#cat seq.txt
1
2
hehe
hehe
4
5
6
7
8
9
10
替换
[root@z1 opt]#sed -i '2chahahaha' seq.txt
[root@z1 opt]#cat seq.txt
1
hahahaha
hehe
hehe
4
5
6
7
8
9
10
取反
[root@z1 opt]#seq 10|sed -n '2!p'
1
3
4
5
6
7
8
9
10
搜索替代
格式:
sed 行范围 s/旧字符串/新字符串/替换标记
替换标记:
- 数字:表明新字符串将替换第几处匹配的地方
- g : 表明新字符串将会替换所有匹配的地方
- p : 打印与替换命令匹配的行,与 -n 一起使用
- w 文件 :将替换的结果写到文件中
修改selinux开机不自启配置文件
[root@z1 opt]#sed -i 's/SELINUX=enabled/SELINUX=disabled/' /etc/selinux/config
修改多行,使用r 以及-e
[root@z1 opt]#sed -ri -e 's/SELINUX=disabled/SELINUX=enabled/' /etc/selinux/config -e 's/SELINUXTYPE=targeted/SELINUXTYPE=111/' /etc/selinux/config
修改全局,后面加g
[root@z1 opt]#sed -i 's/root/admin/g' passwd
分组调用
#分组使用()(),调用第一段使用\1
[root@z1 opt]#echo 11aaxx |sed -r 's/(11)(aa)(xx)/\1/'
11
#调用第2个分组
[root@z1 opt]#echo 11aaxx |sed -r 's/(11)(aa)(xx)/\2/'
aa
#调用所有分组
[root@z1 opt]#echo 11aaxx |sed -r 's/(11)(aa)(xx)/\1\2\3/'
11aaxx
提取IP地址
scss复制代码[root@z1 ~]#ifconfig ens33 |sed -rn '2s/.*(inet) ([0-9.]+) (netmask) ([0-9.]+) (broadcast) ([0-9.]+).*/\2/p'
192.168.254.100
@和/效果一样
less复制代码[root@z1 ~]#echo /etc/sysconfig/network-scripts/ifcfg-ens33 |sed -nr 's@^(.*)/([^/]+)@\2@p'