bash 删除某个字符串后的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46885110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:32:26  来源:igfitidea点击:

Remove everything after a certain string

bashsed

提问by Tomá? Janulík

I though this was going to be a simple task but I am having some trouble with it.

我虽然这将是一项简单的任务,但我遇到了一些麻烦。

Sample input

样本输入

$ cat file.txt
Brown Potatoes with Cheese
Yellow Potatoes with Sugar and Cheese

I would like to use sedto remove everything after the string with

我想用sed字符串后删除一切

So the output would be

所以输出将是

Brown Potatoes 
Yellow Potatoes 

I've tried this regex but it doesn't seem to work.

我试过这个正则表达式,但它似乎不起作用。

sed -i -e "s/with.*//g" file.txt

Where did I go wrong?

我哪里做错了?

回答by mathB

mathb@Balu:~$ sed -i.bak 's/with.*//g' file.txt
mathb@Balu:~$ cat file.txt
Brown Potatoes

Yellow Potatoes
mathb@Balu:~$