Grep匹配仅在“:"之前 [英] Grep match only before ":"
本文介绍了Grep匹配仅在“:"之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我怎样才能只在:
标记之前匹配?
Hello How can I grep only match before :
mark?
如果我运行grep test1 file
,它将显示所有三行.
If I run grep test1 file
, it shows all three lines.
test1:x:29688:test1,test2
test2:x:22611:test1
test3:x:25163:test1,test3
但是我想得到一个输出test1:x:29688:test1,test2
But I would like to get an output test1:x:29688:test1,test2
我将不胜感激.
推荐答案
如果所需的行始终以test1
开头,则可以执行以下操作:
If the desired lines always start with test1
then you can do:
grep '^test1' file
如果它总是跟在:
后面,而不是其他(潜在)匹配项,那么您可以将其作为模式的一部分:
If it's always followed by :
but not the other (potential) matches then you can include it as part of the pattern:
grep 'test1:' file
这篇关于Grep匹配仅在“:"之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文