bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using $ in quotes with characters after it


From: Dan
Subject: Re: Using $ in quotes with characters after it
Date: Fri, 23 Mar 2012 20:52:49 -0400

Hi Paul,

Thanks for your reply.  It seems that trying to simplify my issue seems to
have backfired.  While your answer does indeed work for my simplified
example it doesn't appear to be so with the un-simplified problem.

The required pattern matching is any line starting with "abc" and ending
with a "?".  If this looks like an exam question, that's because it is.
 The professor believed the correct answer was "^abc.*$\?" while I believed
that that was incorrect, and it should be "^abc.*\?$".  Testing my answer
and hers for correctness led me to find this seemingly erroneous behavior.
 Here is a transcript of the input file and patterns with single quotes,
double quotes, and unquoted.  I do realize unquoting your pattern is bad
practice but it does appear to produce the correct output, which is
something I also don't understand as the unquoted pattern should get passed
as "^abc.*?$" or "^abc.*$?".

address@hidden:~$ cat testfile
abc;klj?
abcasdf
ab;kb;

address@hidden:~$ grep '^abc.*$\?' testfile
abc;klj?
abcasdf
address@hidden:~$ grep "^abc.*$\?" testfile
abc;klj?
abcasdf
address@hidden:~$ grep ^abc.*$\? testfile
address@hidden:~$

I did some further experimentation, and the problem may lie with the regexp
itself.

address@hidden:~$ grep "^abc.*\?$" testfile
abc;klj?
abcasdf
address@hidden:~$ grep '^abc.*\?$' testfile
abc;klj?
abcasdf
address@hidden:~$ grep ^abc.*\?$ testfile
abc;klj?
address@hidden:~$

Thanks again,
Daniel Wilson

On Thu, Mar 22, 2012 at 11:38 AM, Paul Eggert <address@hidden> wrote:

> On 03/21/2012 08:16 PM, Dan wrote:
> > Hello,
> >
> > I've noticed something strange when using grep.  It has to do with a
> > pattern similar to this one: "hello$world".
>
> My guess is that your shell is interpreting '$world' as the
> empty string, usually.  (Depends on what identifier you're
> using, 'world' being just one.)  Try using single quotes
> rather than double.
>



-- 
~~ Whatever happens, the sun's still gonna come up tomorrow ~~


reply via email to

[Prev in Thread] Current Thread [Next in Thread]