n0mad
n0mad

Reputation: 9

grep giving incorrect results

I have a text file which contains lines having numeric values at the end of line. When I execute the command cat *.txt | grep 'MOS-LCNB:1.' I expect values only with 1., but for some reason it is showing 100.0.

MOS-LCNB:1.23
MOS-LCNB:1.41
MOS-LCNB:1.83
MOS-LCNB:100.0
MOS-LCNB:1.19
MOS-LCNB:1.39
MOS-LCNB:100.0
MOS-LCNB:100.0

Same problem occurs when I write a python script and perform re.search.

Upvotes: 0

Views: 495

Answers (2)

hwnd
hwnd

Reputation: 70732

That's because . matches any character except line break.

To match a literal period, you need to precede it with a backslash.

grep 'MOS-LCNB:1\.'

Ideone Demo

Upvotes: 2

Amadan
Amadan

Reputation: 198388

grep (and Python re.search) are giving correct results. You need to understand Regular Expressions though.

. means "any character". To match the period, use \.:

cat *.txt | grep 'MOS-LCNB:1\.'

Upvotes: 2

Related Questions