我对以下命令感到困惑
$ cat num.txt
1
2
3
1st
2nd
3th
$ cat num.txt | grep -Eo '[0-9](?:st|nd|th)?'
我认为它应该输出为
1
2
3
1
2
3
但它输出为
1
2
3
1
2nd
3th
我在这里做错了什么?感谢您的帮助。
最佳答案
您可以使用:
grep -Eo '^[0-9]+' file
1
2
3
1
2
3
或者在grep -P
中使用lookahead:
grep -Po '[0-9]+(?=st|nd|th)?' file
1
2
3
1
2
3
关于regex - grep:如何使用非捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314585/