linux 命令 删除文本,linux下用命令删除文本的一句话

由于写脚本需要修改文本文件里的内容,在网上只搜到linux的一个命令 sed

具体用法参数网上有 很多资料,这里只讲一下如何修改和删除内容

[root@localhost binbin]#ls

hello

[root@localhost binbin]#cat hello

binbin

haha

hehe

huhu

[root@localhost binbin]#sed -i 's/binbin/lichang/g' hello

[root@localhost binbin]#cat hello

lichang

haha

hehe

huhu

你会发现,文本里的binbin字符串变成了lichang,

sed -i 是将前面的内容(binbin)替换成后面的内容(lichang)

因此,我们用空格的替代字符串,就达到了我们删除字符串的目的

[root@localhost binbin]#cat hello

binbin

haha

hehe

huhu

[root@localhost binbin]#sed -i 's/binbin/ /g' hello

[root@localhost binbin]#cat hello

haha

hehe

huhu

这时,“binbin”这个字符创就被“ ”代替,效果就是删除了字符串

sed命令里假如有“.”“/”等特殊字符,需要使用"\"进行转义

[root@localhost binbin]#cat hello

binbin

haha

/usr/bin/

/usr/sbin

huhu

[root@localhost binbin]#sed -i 's//usr/bin// /g' hello

sed:-e 表达式 #1,字符 8:“s”的未知选项

[root@localhost binbin]#sed -i 's/\/usr\/bin\// /g' hello

[root@localhost binbin]#cat hello

binbin

haha

/usr/sbin

huhu

本人使用这个命令是由于项目中,需要使用shell脚本,写入~/.bash_profile 启动命令,将软件设置成开机自启动, 但是在卸载的时候需要将~/.bash_profile 里的启动命令删除掉,故使用sed命令用 空格 代字符串,解决了这样的问题

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值