文本替换命令sed (mac环境)

先提供一个文本,主要以hello字符串为主,包含大写类型,一行多个hello等情况。

➜  linux_commands cat hello.txt
hello world
hello boys
she is saying hello
hello hello
HELLO everyone
  • sed (-l) 's/old_text/new_text/(g)' [file]: 输出文件[file]中每行第1个old_text替换成new_text的结果,最后加g表示替换所有匹配。
➜  linux_commands sed -l 's/hello/hi/' hello.txt
hi world
hi boys
she is saying hi
hi hello
HELLO everyone
➜  linux_commands sed 's/hello/hi/' hello.txt
hi world
hi boys
she is saying hi
hi hello
HELLO everyone
➜  linux_commands sed 's/hello/hi/g' hello.txt
hi world
hi boys
she is saying hi
hi hi
HELLO everyone
  • sed -i "" 's/old_text/new_text/' [file]: 替换字符串并写入文件
➜  linux_commands sed -i "" 's/hello/hi/' hello.txt
➜  linux_commands cat hello.txt
hi world
hi boys
she is saying hi
hi hello    -->由于未加g参数,所以第二个hello未替换
HELLO everyone
  • sed -e 's/old_text1/new_text1/' -e 's/old_text2/new_text2/' [file]: 对文件[file]内容将old_text1替换成new_text1或将old_text2替换成new_text2
➜  linux_commands sed -e 's/boys/men/' -e 's/she/he/' hello.txt
hi world
hi men
he is saying hi
hi hello
HELLO everyone
  • sed -n 'line_num,/^$/p' [file]: 打印从行号line_num到下一个空行之间的内容,/p表示打印
➜  linux_commands sed -n '1,/^$/p' hello.txt
hi world
hi boys

➜  linux_commands head -4 hello.txt
hi world
hi boys

she is saying hi

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值