Linux 使用 grep 只匹配一行中的模式一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9543201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use grep to match a pattern in a line only once
提问by Ankur Agarwal
I have this:
我有这个:
echo 12345 | grep -o '[[:digit:]]\{1,4\}'
Which gives this:
这给出了:
1234
5
I understand whats happening. How do I stop grep from trying to continue matching after 1 successful match?
我明白发生了什么。如何在 1 次成功匹配后阻止 grep 尝试继续匹配?
How do I get only
我怎么只得到
1234
采纳答案by MacUsers
You need to do the grouping: \(...\)
followed by the exact number of occurrence: \{<n>\}
to do the job:
您需要进行分组:\(...\)
然后是确切的出现次数:\{<n>\}
来完成这项工作:
maci:~ san$ echo 12345 | grep -o '\([[:digit:]]\)\{4\}'
1234
Hope it helps. Cheers!!
希望能帮助到你。干杯!!
回答by Andrew White
Do you want grep to stop matching or do you only care about the first match. You could use head
if the later is true...
你想让 grep 停止匹配还是只关心第一次匹配。head
如果后者为真,您可以使用...
`grep stuff | head -n 1`
Grep is a line based util so the -m 1
flag tells grep to stop after it matches the first linewhich when combined with head is pretty good in practice.
Grep 是一个基于行的实用程序,因此该-m 1
标志告诉 grep 在匹配第一行后停止,当与 head 结合使用时,这在实践中非常好。
回答by Jonathan Leffler
Use sed
instead of grep
:
使用sed
代替grep
:
echo 12345 | sed -n '/^\([0-9]\{1,4\}\).*/s///p'
This matches up to 4 digits at the beginning of the line, followed by anything, keeps just the digits, and prints them. The -n
prevents lines from being printed otherwise. If the digit string might also appear mid-line, then you need a slightly more complex command.
这最多匹配行首的 4 位数字,后跟任何内容,仅保留数字并打印它们。所述-n
被打印从防止线否则。如果数字字符串也可能出现在中线,那么您需要一个稍微复杂的命令。
In fact, ideally you'll use a (A semi-solution to a considerably more complex problem...now removed!)sed
with PCRE regular expressions since you really need a non-greedy match. However, up to a reasonable approximation, you can use:
事实上,理想情况下您将使用(一种半的解决方案是一个复杂得多的问题...现在删除!)sed
带有 PCRE 的正则表达式,因为您确实需要一个非贪婪匹配。然而,到一个合理的近似,您可以使用:
Since you want the first string of up to 4 digits on the line, simply use sed
to remove any non-digits and then print the digit string:
由于您希望行中的第一个字符串最多为 4 位数字,因此只需使用sed
删除任何非数字,然后打印数字字符串:
echo abc12345 | sed -n '/^[^0-9]*\([0-9]\{1,4\}\).*/s///p'
This matches a string of non-digits followed by 1-4 digits followed by anything, keeps just the digits, and prints them.
这匹配一串非数字后跟 1-4 位数字后跟任何内容,只保留数字并打印它们。
回答by koyae
If – as in your example – your numeric expression will appear at the beginning of the string you're starting with, you could just add a start-of-line anchor ^
:
如果 - 如您的示例 - 您的数字表达式将出现在您开始使用的字符串的开头,您可以添加一个行首锚点^
:
echo 12345 | grep -o '^\([[:digit:]]\)\{1,4\}'
Depending on which exact digits you want, an end-of-line anchor $
might help also.
根据您想要的确切数字,行尾锚点$
也可能有所帮助。
回答by prem
grep manpage says on this topic (see chapter 'regular expressions'):
grep manpage 关于这个主题说(见“正则表达式”一章):
(…)
{n,}
The preceding item is matched n or more times.
{n,m}
The preceding item is matched at least n times, but not more than m times.
(…)
So the answer should be:
所以答案应该是:
echo 12345 | grep -o '[[:digit:]]\{4\}'
I just tested it on cygwin terminal (2018) and it worked!
我刚刚在 cygwin 终端(2018)上对其进行了测试,并且成功了!