Results 1 to 4 of 4

Thread: grep exact match

  1. #1

    Default grep exact match

    Is there any way to grep for an exact match?

    I am looking for lines containing only "68" but grep "68" /etc/services shows a lot more.

    Thanks.

  2. #2

    Default Re: grep exact match

    Quote Originally Posted by henders254 View Post
    Is there any way to grep for an exact match?

    I am looking for lines containing only "68" but grep "68" /etc/services shows a lot more.

    Thanks.
    Try grep -w your_string.

  3. #3

    Default Re: grep exact match

    Quote Originally Posted by henders254 View Post
    Is there any way to grep for an exact match?

    I am looking for lines containing only "68" but grep "68" /etc/services shows a lot more.

    Thanks.
    e.g. grep -w "68" /etc/services

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: grep exact match

    @heders254: The answer given above is correct, but let's look at your phrasing. By exact match, here you meant 68 as a word, for which the -w option is the correct one to use. However in other situations, if you meant you wanted the string "68" (with the double quotes), the problem is one of getting the quotes past the shell. You can do this:

    Code:
    grep '"68"' filename
    or perhaps

    Code:
    grep "\"68\"" filename
    I'll leave it to you to work out how to grep for '68' (with the single quotes).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •