Akshay Kalghatgi
Akshay Kalghatgi

Reputation: 453

Grep exact match returns matches with periods, spaces, hyphens, etc

... | grep -w "2015"
2015%kjlia
hi 2015.halteriafu
etc-2015
...

I want my grep to return lines with "2015" exact matches.

In original data there can be any characters before and after 2015. The above only omitted a few lines like ..yguj2015vj.. with 2015 having adjacent alphanumeric character.

Upvotes: 1

Views: 727

Answers (3)

Ed Morton
Ed Morton

Reputation: 203665

This MAY be what you want:

grep -E '(^|[[:space:]])2015([[:space:]]|$)' file

If not edit your question to clarify.

Upvotes: 3

Sato Katsura
Sato Katsura

Reputation: 3086

Try this:

egrep '(^| )2015( |$)' file.txt

It finds "2015" surrounded by spaces. It doesn't finds "2015" surrounded by tabs though.

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361675

Get rid of the -w. That flag searches for whole words, where a word is 2015 not surrounded by any alphanumeric characters (exactly as you describe).

Or use -x to match the entire line.

Upvotes: 2

Related Questions