user2943926
user2943926

Reputation: 31

how to grep exact match of a string with dots in it in a ksh

I am unable to get the exact matching string as output while trying to grep for a string which has dot in it.

eg: grep "APPLICATION.REFERENCE.LOCAL" <FILENAME>

I have even tried:

grep -F 'APPLICATION.REFERENCE.LOCAL' <FILENAME> which also displays lines with similar matches like APPLICATION.REFERENCE.LOCAL1.

Please, help me.

Upvotes: 3

Views: 4024

Answers (3)

tripleee
tripleee

Reputation: 189799

If you wish to constrain to matches at word boundaries, add -w. So grep -Fw string file

If the definition of "word boundary" is too relaxed for you, you will need to specify the boundary in regex. Something like this, maybe:

grep -E (^|[^A-Z])APPLICATION\.REFERENCE\.LOCAL([^A-Z]|$)' file

will only find matches which are bounded by beginning/end of line, or a character which is not an uppercase alphabetic.

Upvotes: 1

Floris
Floris

Reputation: 46435

You need to escape the dot with a backslash:

grep 'APPLICATION\.REFERENCE\.LOCAL'

and if you want "only and exactly this" in the line, you need to anchor with the "start and end of string" characters:

grep '^APPLICATION\.REFERENCE\.LOCAL$'

Upvotes: 6

devnull
devnull

Reputation: 123608

Since you're using -F which would interpret the pattern as a fixed string, you'd need to supply the -x option to match exactly the whole line.

grep -Fx 'APPLICATION.REFERENCE.LOCAL' filename

From man grep:

   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)

   -x, --line-regexp
          Select  only  those  matches  that exactly match the whole line.
          (-x is specified by POSIX.)

Example:

$ cat file
APPLICATION.REFERENCE.LOCAL
1APPLICATION.REFERENCE.LOCAL
APPLICATION.REFERENCE.LOCAL1
APPLICATION.REFERENCE0LOCAL
$ grep -Fx 'APPLICATION.REFERENCE.LOCAL' file
APPLICATION.REFERENCE.LOCAL

Upvotes: 7

Related Questions