匹配后删除到行尾,保持不匹配 [英] Delete to end of line after a match, keep lines not matched
问题描述
我有以下格式的文件:
interesting text-MIB blah blah blah
VERY INTERESTING TEXT
interesting text-MIB blah blah blah
在包含"-MIB"字符串的每一行中,我要删除此字符串之后的文本,直到该行的末尾.
In each line containing the "-MIB" string, I would like to delete the text following this string, until the end of the line.
以下命令似乎可以正常工作:
The following command seems to work as intended:
sed -i -n -e 's/-MIB.*$/-MIB/p' ./myfile
我的问题是它还会删除找不到我的模式的行.在这里,非常有趣的文字"行将被删除.我对sed并不是很熟悉.我究竟做错了什么? sed是否适合这种治疗方法?
My problem is that it also deletes the lines where my pattern is not found. Here, the line "VERY INTERESTING TEXT" would be deleted. I am not really familiar with sed. What am i doing wrong? Is sed appropriate for this kind of treatment?
推荐答案
您需要省略-n
选项.
查看 http://www.gnu.org/software /sed/manual/sed.html#index-g_t_002d_002dquiet-7
You need to leave out the -n
option.
Check out http://www.gnu.org/software/sed/manual/sed.html#index-g_t_002d_002dquiet-7
这篇关于匹配后删除到行尾,保持不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!