四剑客(sed)
一、 sed
sed简介:
sed是一种流编辑器,它是文本处理中非常好的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件,可以将数据行进行替换、删除、新增、选取等特定工作,简化对文件的反复操作,编写转换程序等。
1.1.语法
sed options script file sed [options] 'command' file(s);
1.2.常用参数
-e :直接在命令行模式上进行sed动作编辑,此为默认选项;(不用写) -f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作; sed -f file === 相当于执行一个bash脚本 -i :直接修改文件内容; 跳过缓冲区 -n :只打印模式匹配的行; -r :支持扩展表达式; -h或--help:显示帮助; -V或--version:显示版本信息。(如果需要用多个命令,要么使用-e选项在命令行中指定,要么使用-f选项在单独的文件中指定。)
1.3.sed常用子命令
|
子命令 |
说明 |
|
a\ |
在当前行下面插入文本; |
|
i\ |
在当前行上面插入文本; ---> iptbales 也是一个概念 |
|
c\ |
把选定的行改为新的文本; |
|
d |
删除,删除选择的行; |
|
D |
删除模板块的第一行; |
|
s |
替换指定字符; |
|
h |
拷贝模板块的内容到内存中的缓冲区; |
|
H |
追加模板块的内容到内存中的缓冲区; |
|
g |
获得内存缓冲区的内容,并替代当前模板块中的文本; |
|
G |
获得内存缓冲区的内容,并追加到当前模板块文本的后面; |
|
l |
列表不能打印字符的清单; |
|
n |
读取下一个输入行,用下一个命令处理新的行而不是用第一个命令; |
|
N |
追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码; |
|
p |
打印模板块的行。 P(大写) 打印模板块的第一行;2p表示打印第二行; |
|
q |
退出Sed; |
|
b |
lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾; |
|
r |
file 从file中读行; |
|
t |
label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾; |
|
T |
label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾; |
|
w |
file 写并追加模板块到file末尾; |
|
W |
file 写并追加模板块的第一行到file末尾; |
|
! |
表示后面的命令对所有没有被选定的行发生作用; |
|
= |
打印当前行号; |
|
# |
把注释扩展到下一个换行符以前; |
1.4.sed常用替换标记(通常与s命令结合使用)
|
命令 |
说明 |
|
g |
表示行内全面替换; |
|
p |
表示打印行; |
|
w |
表示把行写入一个文件; |
|
x |
表示互换模板块中的文本和缓冲区中的文本; |
|
y |
表示把一个字符翻译为另外的字符(但是不用于正则表达式); |
|
\1 |
子串匹配标记; |
|
& |
已匹配字符串标记; |
1.5.sed元字符集
|
字符集 |
说明 |
|
^ |
匹配行开始,如:/^sed/匹配所有以sed开头的行; |
|
$ |
匹配行结束,如:/sed$/匹配所有以sed结尾的行; |
|
. |
匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d; |
|
* |
匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行; |
|
[] |
匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed; |
|
[^] |
匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行; |
|
\(..\) |
匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers; |
|
& |
保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**; |
|
\< |
匹配单词的开始,如:/\ |
|
\> |
匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行; |
|
x\{m\} |
重复字符x,m次,如:/0\{5\}/匹配包含5个0的行; |
|
x\{m,\} |
重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行; |
|
x\{m,n\} |
重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行; |
二、sed案例
2.1.实例1
替换操作:s命令
2.1.1示例1
- 将文本里面的java替换成c++(仅仅显示缓存数据,未写入磁盘,也就是说源数据未修改)
[root@test mnt]# cat test.txt java bash shell python linux oracle mysql [root@test mnt]# sed 's/java/c++/' test.txt #间隔符可以是“/”,“@”,"#" c++ bash shell python linux oracle mysql [root@test mnt]#
2.1.2示例2
- -n选项和p命令一起使用表示只打印那些发生替换的行
[root@test mnt]# sed -n 's#java#c++#p' test.txt c++ [root@test mnt]# cat test.txt java bash shell python linux oracle mysql [root@test mnt]#
2.1.3示例3
- 替换第n位
- 从第n位开始替换
[root@test mnt]# cat test02.txt tsktsktsk tsktsktsk tsktsktsk tsktsktsk tsktsktsk [root@test mnt]# sed 's/ts/TK/2' test02.txt tskTKktsk tskTKktsk tskTKktsk tskTKktsk tskTKktsk [root@test mnt]# sed 's/ts/TK/2g' test02.txt tskTKkTKk tskTKkTKk tskTKkTKk tskTKkTKk tskTKkTKk [root@test mnt]#
2.2.实例2
删除操作:d命令
2.2.1示例1
- 删除空白行
[root@test mnt]# cat test03.txt aaaaaaaa bbbbbbbb cccccccc dddddddd [root@test mnt]# sed '/^$/d' test03.txt aaaaaaaa bbbbbbbb cccccccc dddddddd [root@test mnt]#
2.2.2示例2
- 删除第2行
[root@test mnt]# cat test04.txt [root@test mnt]# sed '2d' test04.txt [root@test mnt]#
2.2.3示例3
- 删除第2行到末尾行
[root@test mnt]# cat test04.txt [root@test mnt]# sed '2,$d' test04.txt [root@test mnt]#
2.2.4示例4
- 删除最后一行
[root@test mnt]# cat test04.txt [root@test mnt]# sed '$d' test04.txt [root@test mnt]#
2.2.5示例5
- 删除开头包含“a”的所有行
[root@test mnt]# cat test05.txt a b c d e a a b c e t a f a a f a l p o [root@test mnt]# sed '/^a/'d test05.txt e a a b c e t a f a a f
2.2.6示例6
- 删除所有包含“a”的行
[root@test mnt]# cat test05.txt a b c d e a a b c e t a f a a f a l p o [root@test mnt]# sed '/a/'d test05.txt [root@test mnt]#
2.3.实例3
已匹配字符串标记
2.3.1示例1
- 正则表达式 \w\+ 匹配每一个单词,使用 [&] 替换它,& 对应于之前所匹配到的单词
[root@test mnt]# cat test06.txt my name is aline.ET [root@test mnt]# sed 's/\w\+/[&]/3' test06.txt my name [is] aline.ET [root@test mnt]# sed 's/\w\+/[&]/g' test06.txt [my] [name] [is] [aline].[ET] [root@test mnt]#
2.3.2示例2
- 替换“1”开头的行(使用&代替前面所选的内容,例如这里是“1”)
[root@test mnt]# cat test07.txt [root@test mnt]# sed -e 's/1/&"ADD"/' test07.txt #默认替换第一个 [root@test mnt]# sed -e 's/1/&"ADD"/2' test07.txt #选择第二个1开始替换 [root@test mnt]# sed -e 's/1/&"ADD"/g' test07.txt #替换全部的1 "ADD" [root@test mnt]#
2.3.3示例3
- 样式匹配到的子串是 10086和abcDE,\(..\) 用于匹配子串,对于匹配到的第一个子串就标记为 \1,依此类推匹配到的第二个结果就是 \2
[root@test mnt]# abcDE abcDE [root@test mnt]# abcDE|sed -e 's#\([0-9]\+\) \([a-Z]\+\)#\2 \1#' abcDE [root@test mnt]# abcDE|sed -e 's#\([0-9]\+\) \([a-z]\+\)#\2 \1#' abc 10086DE [root@test mnt]#
2.3.4示例4
- 替换test123为testdog,命令:\1
[root@test mnt]# cat test08.txt test123 test1234 123test [root@test mnt]# sed -e 's/\(test\)123/\1dog/g' test08.txt #将test标记为“1”,所以1dog=testdog testdog testdog4 123test [root@test mnt]# sed -e 's/test123/testdog/g' test08.txt
2.4.实例4
引用
2.4.1示例1
- sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号
[root@test mnt]# str=string [root@test mnt]# echo $str string [root@test mnt]# echo java STRING java STRING [root@test mnt]# echo java STRING |sed "s/STRING/$str/g" java string [root@test mnt]#
2.4.2示例2
- 选定范围,命令:“,”
[root@test mnt]# cat test09.txt abc def abc def abc def abc def 选定第2到5行打印[root@test mnt]# sed -n '/2/,/5/p' test09.txt abc def abc def abc def 5 从第3行开始打印有c的行 [root@test mnt]# sed -n '3,/c/p' test09.txt abc def abc def 选定2到5行的结尾加上"hahaha" [root@test mnt]# sed '/2/,/5/s/$/hahah/' test09.txt #如果有-n选项的话必须要有-p选项,否则没法打印 abc def abc defhahah abc defhahah abc defhahah 5hahah [root@test mnt]# 对比有np和没有np的区别:没有np是全部打印,并没有选中范围
选定2到5行的结尾加上"hahaha"
[root@test mnt]# sed -n '/2/,/5/s/$/hahah/' test09.txt
[root@test mnt]# sed -n '/2/,/5/s/$/hahah/p' test09.txt
2 abc defhahah
3 abc defhahah
4 abc defhahah
5hahah
[root@test mnt]#
2.5.实例5
多点编辑,命令:e
2.5.1示例1
- 多点编辑命令的顺序对结果有所影响
- 和 -e 等价的命令是 --expression(长格式命令)
[root@test mnt]# cat test10.txt
[root@test mnt]# sed -e 's/5/abc/g' test10.txt
abc
[root@test mnt]# sed -e 's/5/abc/g' test10.txt -e 's/$/ ending/g'
abc ending
ending
[root@test mnt]
2.6.实例6
文件的读写,命令:r、w
2.6.1示例1
文件的写入:sed -n '/test/w test_r' test01.txt 命令解释:将文本test01.txt里面匹配到有“test”的行全部写入test_r里面,如果test_r这个文件存在,则追加内容,如果不存在,则新建文件再写入内容
[root@test mnt]# cat test01.txt
test
test
test
test
a
b test
[root@test mnt]# sed -n '/test/w test_r' test01.txt
[root@test mnt]# cat test_r
test
test
test
test
b test
[root@test mnt]#
2.7.实例7
文件的追加
2.7.1示例1
- 插入行上,命令:i\
- 将hello插入test的行上方
[root@test mnt]# cat test01.txt test test test [root@test mnt]# sed '/test/i\helo' test01.txt helo test helo test helo test [root@test mnt]#
2.7.2示例2
- 行下,命令:a\
- 将hello插入test的行下方
[root@test mnt]# cat test01.txt test test test [root@test mnt]# sed '/test/a\helo' test01.txt test helo test helo test helo [root@test mnt]#
- 插入test的行上方
[root@test mnt]# cat test01.txt 1 test 2 3 test 4 5 test [root@test mnt]# sed '/test/i\helo' test01.txt helo 1 test 2 helo 3 test 4 helo 5 test [root@test mnt]#
2.7.3示例3
- 行下,命令:a\
- 将hello插入第n(n表示1,2,3,4,5......)行
sed 'ni\hello' test.txt -i
2.8.实例8
变形,命令:ny
2.8.1示例1
- 将1到3行的字母abcde转换为ABCDE
[root@test mnt]# cat test01.txt test test test [root@test mnt]# sed -e '1,3y/abcde/ABCDE/' test01.txt tEst tEst test [root@test mnt]#
2.9.实例9
奇偶数
2.9.1示例1
- 打印奇数行
方法一、[root@test mnt]# sed -n 'p;n' test01.txt test test test 方法二、 [root@test mnt]# sed -n '1~2p' test01.txt test test test [root@test mnt]#
2.9.2示例2
- 打印偶数行
方法一、[root@test mnt]# sed -n 'n;p' test01.txt 方法二、 [root@test mnt]# sed -n '2~2p' test01.txt [root@test mnt]#
三、其它练习
打印匹配字符串的下一行
Is test.txt // grep方法
sed -n '/I/{n;p}' test.txt // sed方法
awk '/Is/{getline; print}' test.txt // awk方法
[root@centos7- ~]# cp /etc/passwd ./
[root@centos7- ~]# sed -n 's/^root/#&/p' passwd // 注释掉以root开头的行
[root@centos7- ~]# sed -n -r 's/^root|^ntp/#&/p' passwd // 注释掉以root开头或者以ntp开头的行
[root@centos7- ~]# sed -n '1,5s/^[a-z].*/#&/p' passwd // 注释掉1~5行中以任意小写字母开头的行
[root@centos7- ~]# sed -n '1,5s/^/#/p' passwd // 注释1~5行
[root@centos7- ~]# sed -n 's/^/#/p' passwd // 快速注释一个文件
[root@centos7- ~]# sed -n 's/^#//p' passwd // 快速取消注释
[root@centos7- ~]# sed -n '1,5s/[0-9:/]//gp' passwd // 去掉文件1-5行中的数字、冒号、斜杠或者:
[root@centos7- ~]# sed -n '1,5s/[^a-Z]//gp' passwd
[root@centos7- ~]# sed -n 's/[0-9]/#/pg' passwd // 打印匹配将任意数字替换#
[root@centos7- ~]# sed -n 's/[0-9]/\t/pg' passwd // 打印匹配将任意数字替换成制表符
# 删除vsftpd.conf配置文件里面所有注释的行及空行
[root@centos7- ~]# sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf
或者:
[root@centos7- ~]# sed -r '/^#|^$/d' /etc/vsftpd/vsftpd.conf
或者:
[root@centos7- ~]# sed '/^#/d;/^$/d' /etc/vsftpd/vsftpd.conf
# 使用sed命令截取ip地址
centos7系统
[root@centos7- ~]# ifconfig eth0 |sed -n 's#.*inet \(.*\)netmask.*#\1#p' // 方法一
[root@centos7- ~]# ifconfig eth0 |sed -n '2p' |sed -n 's/.*inet \(.*\)netmask.*/\1/p' // 方法二
centos6系统
[root@centos6- ~]# ifconfig em1 |sed -n 's#.*addr:\(.*\)Bcast.*#\1#p' // 方法一
[root@centos6- ~]# ifconfig em1 |sed -n '2p' |sed -n 's/.*addr:\(.*\)Bcast.*/\1/p' // 方法二
# 使用sed命令一次性截取ip地址、广播地址、子网掩码
[root@centos7- ~]# ifconfig eth0 |sed -n 's#.*inet \(.*\)netmask \(.*\)broadcast \(.*\)#\1\n\2\n\3#p'
[root@centos7- ~]# ifconfig eth0 |sed -n 's#.*inet \(.*\)netmask \(.*\)broadcast \(.*\)#IP地址:\1\n广播地址:\2\n子网掩码:\3#p'
[root@centos6- ~]# ifconfig em1 |sed -n 's#.*addr:\(.*\)Bcast:\(.*\)Mask:\(.*\)#\1\n\2\n\3#p'
四剑客(sed)的更多相关文章
- linux四剑客-grep/find/sed/awk/详解-技术流ken
四剑客简介 相信接触过linux的大家应该都学过或者听过四剑客,即sed,grep,find,awk,有人对其望而生畏,有人对其爱不释手.参数太多,变化形式太多,使用超级灵活,让一部分人难以适从继而望 ...
- shell 编程四剑客简介 find sed grep awk(微信公众号摘抄)
一,Shell编程四剑客之Find 通过如上基础语法的学习,读者对Shell编程有了更近一步的理解,Shell编程不再是简单命令的堆积,而是演变成了各种特殊的语句.各种语法.编程工具.各种命令的集合. ...
- Shell编程四剑客包括:find、sed、grep、awk
一.Shell编程四剑客之Find Find工具主要用于操作系统文件.目录的查找,其语法参数格式为: find path -option [ -print ] [ -exec -ok command ...
- Linux 命令之 linux 四剑客
Linux命令-- 四剑客 一:Linux命令 之 AWK 符号:^ 开头 $ 结尾 awk 是一种处理文本的语言,一个强大的文本分析命令! 1:提取文件中的每行的第二个 提取前文本中内容为 命令: ...
- sed修炼系列(四):sed中的疑难杂症
本文目录:1 sed中使用变量和变量替换的问题2 反向引用失效问题3 "-i"选项的文件保存问题4 贪婪匹配问题5 sed命令"a"和"N" ...
- python 函数“四剑客”的使用和介绍
python函数四剑客:lambda.map.filter和reduce. 一.lambda(匿名函数) 1. 学习lambda要注意一下几点: lambda语句被用来创建新的函数对象,并且在运行的时 ...
- HTML布局四剑客-Flex,Grid,Table,Float
前言 在HTML布局中有很多的选择,同一种表现方式可以使用不同的方法来实现.下面来对四种最常见的布局方式进行阐述和解释,它们分别是Float,Table,Grid和Flex Float 第一位出场的就 ...
- Flask - 四剑客 | templates | 配置文件 | 路由系统 | CBV
Flask框架简介 说明:flask是一个轻量级的web框架,被称为微型框架.只提供了一个高效稳定的核心,其它全部通过扩展来实现.意思就是你可以根据项目需要进行量身定制,也意味着你需要不断学习相关的扩 ...
- Flask 四剑客
Flask 四剑客 返回字符串,返回 html , 跳转路由,返回 json from flask import Flask, render_template, redirect, jsonify a ...
- Linux四剑客find/grep/awk/sed
find ./ -name "*txt" -maxdepth 1 -type f -mtime -2 -exec mv {} ./bbb.txt \; 这条命令表示找当前目录(-m ...
随机推荐
- 2014 3.22 校队选拔——A
依然非常失望,我为什么现在还是那么弱,今天就做出了一道题,垫底. 一个大家都看出来的C题,我居然没找到规律,想了一会儿就放弃了. A题是这样的,有n种珍珠,给出这n种珍珠各自的数目,再给出一个M,表示 ...
- POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...
- windows下python自带的pip安装速度过慢解决方案
自带下载地址为国外源下载速度时常在20KB以内切换为国内源直接满速! 国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn ...
- atomic一定线程安全吗
atomic只是保证了操作的原子性,原子操作即一个操作不可再分. atomic只是对读写操作进行了加锁,保证了多线程开发时一个读写操作完成之后才能进行下一个读写操作 atomic和线程安全没有太大的关 ...
- Object中有哪些公共方法及作用
大家在学习java的时候,一定遇到过Object类,因为在java单一继承体系中Object类是根类,所有的类都会继承它,并拥有Object的公共方法,意味着在java的面向对象的世界中,所有对象都拥 ...
- UML-类图定义
1.之前,学习过领域模型(概念模型),与类图的区别: 1).属于OOA 2).没有方法,只有属性和关联 类图属于OOD,属于静态对象建模 2.例子 3.类元包含哪些? 1).类(抽象类) 2).接口
- Java之多线程窗口卖票问题(Thread)
/** * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式 * * 存在线程的安全问题,待解决. * */class Window extends Thread{ priv ...
- quartz定时定时任务执行两次
quartz框架没问题. 流程: sping-quartz配置 <?xml version="1.0" encoding="UTF-8"?> < ...
- 关于nginx配置的一个报错connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)
针对配置php的情况: linux服务器一般提示这个 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) ...
- mac 编程环境
新mac (EI Capitan),需要在python中使用xgboost,通过pip安装未成功. 配置pip cat $HOME/Library/Application\ Support/pip/p ...