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

あなたは*何にでもマッチするシェルのワイルドカードを考えています。正規表現では、a*はその直前にあるもの (この場合は ) の「0 個以上」を意味する量指定3です。

したがって、式は、113その後に 0 個以上3の s が続くことを意味します。

于 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 に答える