Interesting. I missed that part of the man page where it says "?" loses its meaning in basic grep. Thanks for your time in helping me figure out where exactly I went wrong.
On Sat, Mar 24, 2012 at 9:05 PM, Mike Frysinger <[email protected]> wrote: > On Saturday 24 March 2012 15:06:00 Dan wrote: > > This is the output after running `set -x`. It looks like the shell is > > behaving properly. It's un-escaping the question mark when it's supposed > > to be and it isn't when it isn't supposed to be. Grep shouldn't be > > producing that output though. The only explanation I can think of is > that > > the wildcard is being too greedy and consuming all of the string. But > from > > what I know that's not how it works. > > seems correct to me. in the first two, grep gets a question mark with a > backslash. in the last one, it doesn't because the shell processed it > first. > if you read the man page, it says that in a basic regular expression (which > you're doing because you didn't use -E or run egrep), a plain "?" has no > meaning -- it's just another character. grep needs to see the backslash to > treat it specially. further, the anchor characters ("$" and "^") only have > meaning if they are in the last or first position respectively. otherwise, > they're just another character to match. > > hence, this command: > grep '^abc.*$\?' testfile > means "anchor to the start of the line, then match abc, then match > anything, > then either anchor to the end of the line or don't". so really, "$\?" at > the > end of the regexp is completely pointless. > > but this command: > grep ^abc.*$\? testfile > means "anchor to the start of the line, then match abc, then match > anything, > then match a dollar sign, then match a question mark". > -mike > -- ~~ Whatever happens, the sun's still gonna come up tomorrow ~~
