3

考虑这个文本文件:

TEST FILE : test                         #No match
                                         #No match
Run grep "1133*" on this file            #Match
                                         #No match
This line contains the number 113.       #Match
This line contains the number 13.        #No match
This line contains the number 133.       #No match
This line contains the number 1133.      #Match
This line contains the number 113312.    #Match
This line contains the number 1112.      #No match
This line contains the number 113312312. #Match
This line contains no numbers at all.    #No match

该命令如何grep评估1133*正则表达式?

echo "Consider this text file:

TEST FILE : test                         #No match
                                         #No match
Run grep \"1133*\" on this file          #Match
                                         #No match
This line contains the number 113.       #Match
This line contains the number 13.        #No match
This line contains the number 133.       #No match
This line contains the number 1133.      #Match
This line contains the number 113312.    #Match
This line contains the number 1112.      #No match
This line contains the number 113312312. #Match
This line contains no numbers at all.    #No match" | grep "1133*"

输出:

Run grep "1133*" on this file            #Match
This line contains the number 113.       #Match
This line contains the number 1133.      #Match
This line contains the number 113312.    #Match
This line contains the number 113312312. #Match

为什么该行包含113正数?

正则表达式是否1133*意味着除了查找包含单词的所有行之外的任何其他内容1133+anything else

此示例可在tldp regexp文档页面上找到。

4

2 回答 2

8

你正在考虑一个 shell 通配符,它​​可以*匹配任何东西。在正则表达式中,a*是一个量词,表示它之前的任何内容的“零个或多个”,在这种情况下是3.

所以你的表达意味着113后跟零个或多个3s。

于 2012-08-14T13:23:37.817 回答
1

尝试 grep "1133$" 或 grep "^1133$"

其中 ^ 是行首, $ 是行尾

如果您的行假设 3 列:aaa 113 bbbb

cat file.txt|awk '{print $2}'|grep "^1133$"|wc -l

确保您只查看特定列

于 2012-08-14T15:28:12.947 回答