问题描述
我正在使用一个小的bash脚本.我需要删除仅与该行的第一个单词中的确切单词匹配的整行.
I'm working in a small bash script. I need to remove an entire line that match an exact word in only the first word of the line.
这就是文本文件的外观:
That's how the text file looks:
John is a dumb
Maria is awesome
Toni and Maria are funny
我要删除第二行.
现在,我可以匹配精确的单词"maria",但它也删除了第三行:
Right now, I can match the exact word "maria" but it removes the third line too:
sed -i "/\b\(maria\)\b/d" file.txt
如何仅指定第一个单词?
How to specify just the first word?
谢谢!
推荐答案
当前,您正在查找被单词边界(由 \ b
表示)包围的玛丽亚".注意,我还删除了不必要的括号并添加了/I
标志,这将使搜索不区分大小写.您的原件不会匹配"Maria".
Currently you are looking for "maria" surrounded by a word boundary (represented by \b
.) Instead look for "maria" preceded by the start of line (represented by ^
.) Note I've also removed unnecessary parentheses and added the /I
flag, which will make the search case-insensitive. Your original wouldn't have matched "Maria".
sed -i "/^maria\b/Id" file.txt
将不区分大小写的标志固定为I而不是i!
fixed case insensitive flag to I instead of i!
这篇关于"sed"命令删除与第一个单词的确切字符串匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!