Results 1 to 8 of 8

Thread: grep question

  1. #1
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Question grep question

    Hello all,

    Is grep treating special the fallowing characters: [ or ] cause I made a grep with a string that contains [ ]say:

    grep '<</asasa[2313]sasa' file and I didn't find nothing but if I grep with:
    grep '<</asasa file
    it finds that line and the line contains [ ] characters.

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

    Default Re: grep question

    [] are used to enclose a character class. [abc] means any of the characters a, b, or c in that position. And you can guess that [0-9] means any digit. If you wanted a literal [ you should precede it with \.

    The metacharacters that you should watch out for are explained in the grep man page.

  3. #3
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: grep question

    I know that [ ] are for regexp but I thought that if I put [ or ] between '' the special meaning is eliminated. Is there a workaround for treating all characters in the searched string liberally? I mead a special flag for grep command. Or how can I do this without escaping the meta characters with sed or something else?

    Quote Originally Posted by ken_yap View Post
    [] are used to enclose a character class. [abc] means any of the characters a, b, or c in that position. And you can guess that [0-9] means any digit. If you wanted a literal [ you should precede it with \.

    The metacharacters that you should watch out for are explained in the grep man page.

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,761

    Default Re: grep question

    The " characters are used by the shell to escape e.g. white space. In that process they are "Eaten" by the shell and the resulting string is given as an argument field to the program called (grep in this case). So grep will never see these ".

    When you want " character in a string given as a argument field to a program (which is not something that will help you here) you must escape the ", e.g. by putting a \ before the ".
    Code:
    henk@boven:~> echo "\""
    "
    The same as you want to put a \ in the argument. It must be escaped, because the shell eats it:
    Code:
    henk@boven:~> echo \\[
    \[
    See how we grep a [ in the file aap
    Code:
    henk@boven:~> cat aap
     aap [ noot
    henk@boven:~> grep [ aap
    grep: Ongeldige reguliere expressie
    henk@boven:~> grep \[ aap
    grep: Ongeldige reguliere expressie
    henk@boven:~> grep \\[ aap
     aap [ noot
    henk@boven:~>
    or with ' escape in the shell and \ in grep:
    Code:
    henk@boven:~> grep '\[' aap
     aap [ noot
    Nice isn't it? one can go and try all sorts of combinations for hours and hours ........
    Henk van Velden

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

    Default Re: grep question

    Quote Originally Posted by ionpetrache View Post
    I know that [ ] are for regexp but I thought that if I put [ or ] between '' the special meaning is eliminated. Is there a workaround for treating all characters in the searched string liberally? I mead a special flag for grep command. Or how can I do this without escaping the meta characters with sed or something else?
    No, the shell eats the quote characters. You need some kind of escaping or quoting at the shell, otherwise the [ is special to the shell. So you have two stages to think about, 1. how do I get these characters to the program the way I want, and 2. how to tell the program that [ is not special. Let's try it with just the echo program.

    Code:
    echo \\[
    echo '\['
    echo "\["
    will all get \[ to echo.

    After some practice you get used to this.

  6. #6
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: grep question

    My problem is that the search string is given by a script so.. I don't know how it's look like. I need an option or another utility that doesn't interpret regexp characters.

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

    Default Re: grep question

    If you just want to use grep as a simple matcher, then look at the --fixed-strings option. This turns off regexp interpretation of the characters and the only special character is the newline, which separates strings. But it also means you can search for more than one string at once.

    You will still have to get any shell metacharacters past the shell. If you single quote the whole string, then the only special character is the single quote itself which you will have to escape as this sequence: '"'"'. Explaination: end the single-quoted string, start a double-quoted string, real single-quote, end the double-quoted string, and start a new single-quoted string. So you only need to escape the single quote this way before giving it to grep --fixed-strings.

    If you are calling grep from a program (e.g. C, Perl, Python, etc) it is possible to avoid calling the shell, and then you don't have to worry about escaping single-quotes.

  8. #8
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: grep question

    Thanks! the -F flag is just what I was looking for.

    Quote Originally Posted by ken_yap View Post
    If you just want to use grep as a simple matcher, then look at the --fixed-strings option. This turns off regexp interpretation of the characters and the only special character is the newline, which separates strings. But it also means you can search for more than one string at once.

    You will still have to get any shell metacharacters past the shell. If you single quote the whole string, then the only special character is the single quote itself which you will have to escape as this sequence: '"'"'. Explaination: end the single-quoted string, start a double-quoted string, real single-quote, end the double-quoted string, and start a new single-quoted string. So you only need to escape the single quote this way before giving it to grep --fixed-strings.

    If you are calling grep from a program (e.g. C, Perl, Python, etc) it is possible to avoid calling the shell, and then you don't have to worry about escaping 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
  •