如何全局替换不以特定模式开头的行中的字符串 [英] How to globally replace strings in lines NOT starting with a certain pattern

查看:38
本文介绍了如何全局替换不以特定模式开头的行中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sed 将字符串 foo 全局替换为字符串 bar.这应该只对不以字符串##Input 开头的行执行.

I want to globally replace the string foo with the string bar, using sed. This should only be done for lines which do NOT start with the string ##Input.

我无法让它工作.我尝试过这样的事情,但达到了我不确定我是否知道自己在做什么的地步:

I can't get it to work. I tried things like this but reached a point where I'm not sure if I know what I'm doing:

sed -i '/^##Input/ s/foo/bar/g' myfile

请帮忙!

推荐答案

你只需要使用 ! 来否定匹配:

You just need to negate the match using !:

sed -i '/^##Input/! s/foo/bar/g' myfile

这篇关于如何全局替换不以特定模式开头的行中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆