Shell之Sed命令-yellowcong

Sed可以用来替换文本,sed -i '/xx/xxx/p' file来替换文件类容,-i表示更改文件,如果不加上参数-i,只是替换了,但是没有写入到文件里面。还有,路径的替换是比较特殊的,需要特别的注意

替换命令

替换路径

Mac上使用sed命令时,报出sed: 1: “1.txt”: invalid command code .错误。是由于Mac上sed命令与linux下稍有不同。Mac上默认提供修改时的备份机制。

sed -i "" "s#E:\\\\开发\\\\yellow\\\\images\\\\#../images/#g" ./README.md

#需要备份的情况
sed -i ".bak" 's/string_old/string_new/g' grep -rl 'string_old' ./

正则匹配替换

#将<File-Output-Dir>/tmp/test</File-Output-Dir>  替换为指定配置
#匹配<File-Output-Dir> </File-Output-Dir> 里面所有的配置 ,.* 表示匹配任意长度的任意字符
sed -i "s#<File-Output-Dir>.*</File-Output-Dir>#<File-Output-Dir>/home/test</File-Output-Dir>#g" *.xml;

^

锚点行首的符合条件的内容,用法格式"^pattern"

$

锚点行首的符合条件的内容,用法格式"pattern$"

^$

空白行

.

匹配任意单个字符

*

匹配紧挨在前面的字符任意次(0,1,多次)

.*

匹配任意长度的任意字符

\?

匹配紧挨在前面的字符0次或1次

\{m,n\}

匹配其前面的字符至少m次,至多n次

\{m,\}

匹配其前面的字符至少m次

\{m\}

精确匹配前面的m次\{0,n\}:0到n次

\<

锚点词首----相当于 \b,用法格式:\<pattern

\>

锚点词尾,用法格式:\>pattern

\<pattern\>

单词锚点

 

分组,用法格式:pattern,引用\1,\2

[]

匹配指定范围内的任意单个字符

[^]

匹配指定范围外的任意单个字符

[:digit:]

所有数字, 相当于0-9, [0-9]---> [[:digit:]]

[:lower:]

所有的小写字母

[:upper:]

所有的大写字母

[:alpha:]

所有的字母

[:alnum:]

相当于0-9a-zA-Z

[:space:]

空白字符

[:punct:]

所有标点符号

全面替换标记g

使用后缀 /g 标记会替换每一行中的所有匹配:

当需要从第N处匹配开始替换时,可以使用 /Ng:

#替换所有的name
echo name_xx_name_xx2 | sed 's/name/NAME/g'

#替换第二次的name为大写
echo name_xx_name_xx2 | sed 's/name/NAME/2g'

查看结果,可以看到,这个 g可以替换当前行的第几个

这里写图片描述

路径替换

zoo_sample.cfg配置文件,实验替换文件

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

路径的替换,需要结合#好表示替换的类容,而不是分号来替换

#查找/tmp/zookeeper 的文件
sed -n '/\/tmp\/zookeeper/p' test.cfg

#替换路径的操作
sed -i 's#/tmp/zookeeper#/tmp/zookeeper1/data#' test.cfg

这里写图片描述

追加行

#在/dataDir/ 后面添加一行
sed -i '/^dataDir/a\dataLogDir=\/tmp\/zookeeper1\/logs' test.cfg

#在dataDir 前面添加一样
sed -i '/^dataDir/i\dataLogDir=\/tmp\/zookeeper1\/logs' test.cfg

在后面添加数据,参数是 a(after),
这里写图片描述

在前面添加数据,参数是i(insert)
这里写图片描述

文件头/尾追加

#追加的数据太长了,所以使用了 \ 进行了换行的操作
sed -i '$a \server.1=127.0.0.1:2222:2225 \
	server.2=127.0.0.1:3333:3335 \
	server.3=127.0.0.1:4444:4445 \
	' test.cfg

#在文件的第一行添加文件
sed -i '1 i\fisrt file' test.cfg

添加成功
这里写图片描述

最后一行添加

删除命令

删除的操作,可以删除哪一行,也可以设定匹配删除哪一行的数据

行删除

#删除第一行
sed -i '1d' test.cfg

#删除第二行,第三行,第四行有此类推
sed -i '2d' test.cfg

#删除最后一行
sed -i '$d' test.cfg

这里写图片描述

匹配删除

sed -i '//d' test.cfg

这里写图片描述

查找命令

查找文件行数据


#查找文件的第二行
sed -n  '2p' zoo.cfg

#打印文件的1-6 行的数据
sed -n '1,6p' zoo.cfg

#打印以data开头的行
sed -n '/^data/p' zoo.cfg

这里写图片描述

参考文章

http://www.cnblogs.com/ctaixw/p/5860221.html
http://man.linuxde.net/sed#sed%E6%9B%BF%E6%8D%A2%E6%A0%87%E8%AE%B0
http://blog.csdn.net/loveaborn/article/details/17269645

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值