Find the answer to your Linux question:
Results 1 to 8 of 8
what does the following command sequence means ? grep "d^c" What does it will get matched ?...
  1. #1
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97

    doubt in grep regular expression

    what does the following command sequence means ?

    grep "d^c"

    What does it will get matched ?

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    It matches any strings containing "d^c", for example:
    echo "d^c" | grep "d^c"
    echo "ABCd^cMM" | grep "d^c"


    When ^ is not the first character in the expression, it is just a literal ^, otherwise it means the beginning of the line (when there are no square brackets around).

    Read GREP for more examples.

  3. #3
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    Thanks for the same.


    I understood that, if the ^ does not occurs at first it will be taken literal..


    But now i have strange problem that, it works like what you said in one server, but does not works in another server. I checked the version, and both are same 2.5.1. So what could be the issue.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Actually I got similar results after you mentioned in your last post. I had it working on
    OpenSuSE 10.3 (grep version 2.5.2) and Ubuntu 8.04 (grep 2.5.3)
    but not OpenSuSE 10.2. (grep 2.5.1)
    Now I wonder the same question.

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    I suspect that behaviour does not depend on the grep version, but rather on the locale settings:

    Code:
    $ export LC_ALL=en_US
    $ grep "b^gg" testinput
    >
    $ export LC_ALL=en_US.UTF-8
    $ grep "b^gg" testinput
    > b^gg

  6. #6
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Quote Originally Posted by burschik View Post
    I suspect that behaviour does not depend on the grep version, but rather on the locale settings:

    Code:
    $ export LC_ALL=en_US
    $ grep "b^gg" testinput
    >
    $ export LC_ALL=en_US.UTF-8
    $ grep "b^gg" testinput
    > b^gg
    Not really. On my Ubuntu 8.04. grep matches no matter whatever LC_ALL setting is. Same story with my OpenSUSE 10.3.

  7. #7
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181

  8. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Good research on the reported bug.

    I would consider character caret (^) to be special enough that I would escape it. In doing so, you have portability among programs and versions. Here are a few trial variations:
    Code:
    #!/bin/bash -
    
    # @(#) s1       Demonstrate handling of caret (^) in [e]grep, perl.
    
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) grep perl
    set -o nounset
    echo
    
    echo " Results with grep:"
    echo "1d^c" | grep "d^c"
    echo "2d^c" | grep "d\^c"
    echo "3d^c" | grep "d[^]c"
    echo "4d^c" | grep -U "d^c"
    
    echo
    echo " Results with egrep:"
    echo "1d^c" | egrep "d^c"
    echo "2d^c" | egrep "d\^c"
    echo "3d^c" | egrep "d[^]c"
    echo "4d^c" | egrep -U "d^c"
    
    echo
    echo " Results with perl:"
    echo "1d^c" | perl -wp -e '/d^c/;'
    echo "2d^c" | perl -wp -e '/d\^c/;'
    echo "3d^c" | perl -wp -e '/d[^]c/;'
    
    exit 0
    Producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    Linux 2.6.11-x1
    GNU bash 2.05b.0
    grep (GNU grep) 2.5.1
    perl 5.8.4
    
     Results with grep:
    2d^c
    grep: Invalid regular expression
    
     Results with egrep:
    2d^c
    grep: Invalid regular expression
    
     Results with perl:
    1d^c
    2d^c
    Unmatched [ in regex; marked by <-- HERE in m/d[ <-- HERE ^]c/ at -e line 1.
    I also ran this script with GNU grep 2.5.3 (on GNU/Debian lenny) and the line "2d^c" was matched (some others as well, showing the bug to have been fixed there).

    So if you cannot change grep, then I would recommend using the \^ sequence or use a one-liner perl script, at least for the near future ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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