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

标签 bash sed

<分区>

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

示例输入

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

我想使用 sed 删除字符串 with 之后的所有内容

所以输出是

Brown Potatoes 
Yellow Potatoes 

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

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

我哪里出错了?

最佳答案

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

Yellow Potatoes
mathb@Balu:~$

关于bash - 删除某个字符串后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46885110/

相关文章:

regex - 在 bash 中使用从行的中间或末尾提取单词

mysql - 使用 awk 或 sed 将 mysql.log 重新格式化为简单格式

linux - (SED) : code works ok via terminal, 但不在 bash 脚本中。我应该 "escape"什么?

bash - 将 csv 文件中的第一列从时间戳转换为年月格式

linux - 在左花括号和右花括号之间获取字符串

linux - Bash:在 .txt 文件中搜索和替换密码

regex - 如何在 Ubuntu 中使用 bash 反转文件中的所有单词?

bash - 如何使用 shell、bash、awk、sed 在稀疏矩阵中插入零元素

bash - 如何遍历kubectl的节点列表,并回显每个节点名称?

bash - 使用 Bash 计算程序的平均执行时间