Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 397
Lucian Enache Avatar asked Nov 23 '25 10:11

Lucian Enache


2 Answers

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.

like image 94
Wiseguy Avatar answered Nov 26 '25 00:11

Wiseguy


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

like image 25
V H Avatar answered Nov 26 '25 00:11

V H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!