Results 1 to 8 of 8
what does the following command sequence means ?
grep "d^c"
What does it will get matched ?...
- 10-14-2008 #1
doubt in grep regular expression
what does the following command sequence means ?
grep "d^c"
What does it will get matched ?
- 10-14-2008 #2Linux 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.
- 10-15-2008 #3
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.
- 10-15-2008 #4Linux 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.
- 10-15-2008 #5Linux 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
- 10-15-2008 #6Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
- 10-15-2008 #7Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Alright then. It is this bug:
grep - Bugs: bug #9519, echo do^re | grep do^re; fails... [Savannah]
- 10-15-2008 #8Linux Engineer
- 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:
Producing: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
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).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.
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, drlWelcome - 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 )


Reply With Quote
