Lucian Enache
Lucian Enache

Reputation: 2530

Grep with regular expressions

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

How does the grep command evaluate the 1133* regular expression?

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*"

Outputs:

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

Why is the line containing 113 a positive?

Is the regular expression 1133* meant to mean anything else than find all lines that contain the word 1133+anything else?

This example was found on the tldp regexp documentation page.

Upvotes: 3

Views: 982

Answers (2)

V H
V H

Reputation: 8587

Try grep "1133$" or grep "^1133$"

where ^ is the start of the line and $ is the end of the line

If your line was assuming 3 columns : aaa 113 bbbb

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

To ensure you are only looking at the specific column

Upvotes: 1

Wiseguy
Wiseguy

Reputation: 20873

You're thinking of a shell wildcard, where * matches anything. In regular expressions, a * is a quantifier that means "zero or more" of whatever immediately precedes it, which in this case is 3.

So your expression means 113 followed by zero or more 3s.

Upvotes: 8

Related Questions