Grep 只在“:"之前匹配 [英] Grep match only before ":"
本文介绍了Grep 只在“:"之前匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我如何才能只在 :
标记之前进行 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
我希望得到任何建议.
推荐答案
如果所需的行总是以 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屋!
查看全文