部份内容参考以下连接
https://www.runoob.com/linux/linux-comm-sed.html?hmsr=toutiao.iohtml
表示点所在位置能够表示任意一个字符,linux
咱们作一个测试文本正则表达式
cp /etc/passwd test.txt
在test.txt 行首加一行raaoshell
sed -i '1i'\raao test.txt grep 'r.o' test.txt
要想获取raao 行,可使用
{n} 表示匹配前面字符n次egrep 'r.{2}o' test.txt
ide
编辑 test.txt 文件加入三行, rooot,rot,rt测试
sed -i '1i'\ rooot test.txt sed -i '1i'\ rot test.txt sed -i '1i'\ rt test.txt
egrep 'roo?t' test.txt
3d
egrep 'ro+t' test.txt
code
egrep 'ro*t' test.txt
htm
sed 用法补充
动做说明:
a :新增, a 的后面能够接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面能够接字串,这些字串能够取代 n1,n2 之间的行!
d :删除,由于是删除啊,因此 d 后面一般不接任何咚咚;
i :插入, i 的后面能够接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。一般 p 会与参数 sed -n 一块儿运行~
s :取代,能够直接进行取代的工做哩!一般这个 s 的动做能够搭配正规表示法!例如 1,20s/old/new/g 就是啦!blog
打印第一行sed -n 1p test.txt
打印前两行sed -n 1,2p test.txt
删除前3行后打印10行sed 1,3d test.txt |head
查询后删除sed '/root/d' test.txt |head
能够分为a在当前行的下一行插入,i 在当前行的上一行插入
在文本开头插入sed '1i'\up test.txt
在文本结尾插入sed '$a'\down test.txt
取代前4行内容为replace1-4输出sed '1,4c replace1-4' test.txt
作一个全局替换将nologin替换为longoutsed 's/nologin/longout/g' test.txt
数据查找并替换ifconfig ens33 |grep '\binet\b' |sed 's/^.*inet//g' |sed 's/netmask.*$//g'
至关于ifconfig ens33 |grep '\binet\b' |awk '{print $2}'
备注:grep' \b\b' \b单词锁定符,如: '\binet\b'只匹配inet,至关于-w 选项