使用grep只匹配一行中的一个模式 [英] Use grep to match a pattern in a line only once

查看:184
本文介绍了使用grep只匹配一行中的一个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

  echo 12345 | grep -o'[[:digit:]] \ {1,4 \}'

这给出了:

  1234 
5

我了解最新情况。



我怎样才能在1次成功比赛后继续匹配?

$ p> 1234


解决方案

您需要进行分组: \(... \),后面是确切的出现次数: \ {< n> \\ \\} 来完成这项工作:

  maci:〜san $ echo 12345 | grep -o'\([[:digit:]] \)\ {4 \''
1234

希望它有帮助。干杯!


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?

How do I get only

1234

解决方案

You need to do the grouping: \(...\) followed by the exact number of occurrence: \{<n>\} to do the job:

maci:~ san$ echo 12345 | grep -o '\([[:digit:]]\)\{4\}'
1234

Hope it helps. Cheers!!

这篇关于使用grep只匹配一行中的一个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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