如何让grep在第一场比赛中停下来? [英] How to make grep stop at first match on a line?

查看:136
本文介绍了如何让grep在第一场比赛中停下来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个文件test.txt

 
#test.txt
odsdsdoddf112 test1_for_grep
dad23392eeedJ test2 for grep
Hello World测试
垃圾

我想提取后面有空格的字符串。我使用了下面的表达式并且它能够工作:

 
grep -o [[:alnum:]] *。[[:blank:]] test。 txt

它的输出是

 
odsdsdoddf112
dad23392eeedJ
test2

您好
全球

但问题是grep打印所有已经获得空间的字符串,因为我希望它在首行匹配后停止,然后进入第二行。



我应该使用哪种表达方式在这里使用,为了使它在第一次比赛后停止并转移到下一行?

这个问题可以用gawk或其他工具解决,但我会欣赏仅使用grep的解决方案。



编辑
我在Linux系统上使用GNU grep 2.5.1,如果相关的话。



编辑



在下面给出的答案的帮助下,运气与

pre
grep -o ^ [[:alnum:]] * test.txt
grep -Eo ^ [[:: alnum:]] + test.txt

两者都给了我正确的答案。



现在令我惊讶的是我试过使用

 
grep -Eo ^ [[:alnum:]] + [[:blank:]]test.txt

建议这里但没有得到正确的回答。
这里是我的终端上的输出

 
odsdsdoddf112
dad23392eeedJ
test2
for
您好
全球

但RichieHindle和Adrian Pronk的评论显示,他们在系统上获得了正确的输出。任何人有一些想法,为什么我也没有得到我的系统相同的结果。任何想法?任何帮助将不胜感激。

编辑

好的,grep 2.5.1有一些错误,因为我的输出不正确。我安装了grep 2.5.4,现在它工作正常。请参阅此链接了解详情。

没有主要的空格,添加一个 ^ 只匹配一行的开头,并将 * 更改为 + 只有当您有一个或多个字母数字字符时才匹配。 (这意味着添加 -E 来使用扩展正则表达式)。

  grep -Eo^ [[:alnum:]] + [[:blank:]]test.txt 

(我也从中间删除了;我不确定那是干什么的)


Well, I have a file test.txt

#test.txt
odsdsdoddf112 test1_for_grep
dad23392eeedJ test2 for grep
Hello World test
garbage

I want to extract strings which have got a space after them. I used following expression and it worked

grep -o  [[:alnum:]]*.[[:blank:]]  test.txt

Its output is

odsdsdoddf112
dad23392eeedJ 
test2 
for    
Hello 
World

But problem is grep prints all the strings that have got space after them, where as I want it to stop after first match on a line and then proceed to second line.

Which expression should I use here, in order to make it stop after first match and move to next line?

This problem may be solved with gawk or some other tool, but I will appreciate a solution which uses grep only.

Edit I using GNU grep 2.5.1 on a Linux system, if that is relevant.

Edit

With the help of the answers given below, I tried my luck with

grep  -o   ^[[:alnum:]]*  test.txt
grep  -Eo  ^[[:alnum:]]+  test.txt

and both gave me correct answers.

Now what surprises me is that I tried using

grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt

as suggested here but didn't get the correct answer. Here is the output on my terminal

odsdsdoddf112
dad23392eeedJ 
test2 
for    
Hello 
World

But comments from RichieHindle and Adrian Pronk, shows that they got correct output on their systems. Anyone with some idea that why I too am not getting the same result on my system. Any idea? Any help will be appreciated.

Edit

Well, it seems that grep 2.5.1 has some bug because of which my output wasn't correct. I installed grep 2.5.4, now it is working correctly. Please see this link for details.

解决方案

If you're sure you have no leading whitespace, add a ^ to match only at the start of a line, and change the * to a + to match only when you have one or more alphanumeric characters. (That means adding -E to use extended regular expressions).

grep -Eo "^[[:alnum:]]+[[:blank:]]" test.txt

(I also removed the . from the middle; I'm not sure what that was doing there?)

这篇关于如何让grep在第一场比赛中停下来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆