What is the use of ? in grep command.. And practical use

4

1

I tried to find the actual practical use of ? i.e. for e.g. "egrep a? filename" but was not able to find any.. It returns all results..

So, Please help me out wherein i could know the actual use of egrep ? command..

If i use 'a?', it returns all result i.e. strings or lines. which has 0 a's, 1 a's, 2 a's and so on.. i.e. i am not able to find the use of the same..

Thanks

RBA

Posted 2009-08-15T15:13:41.740

Reputation: 239

Answers

8

Say you wanted to match numeric assignment expressions like this in a script:

x=1234

where some numbers are negative and have a minus sign:

x=-5678

You could use this:

grep -E "x=-?[0-9]+" *

The question mark makes the minus optional.

(I don't think plain grep supports ? or +, hence -E).

RichieHindle

Posted 2009-08-15T15:13:41.740

Reputation: 702

I understood it.. Thanks a lot.. Also what does 'egrep a? filename' will do actually.. If you can put it in words.. – None – 2009-08-15T16:28:44.483

3@RB: egrep a? filename will match every line. It's not useful on its own. – RichieHindle – 2009-08-15T16:48:59.460

2Regular GNU grep (without the -E) supports the ? but you must escape it.

echo "color" | grep "colou\?r"

Should return color. – Matt Kneiser – 2014-01-21T19:53:36.890

6

? is not a metacharacter in basic grep, so if you're grepping for a?, you are in fact grepping for a followed by a question mark.

If you were using egrep (aka grep -E), then answers indicating that ? is a zero-or-one-of-previous-entity regex metacharacter would be correct.

chaos

Posted 2009-08-15T15:13:41.740

Reputation: 1 599

Yes sir, u are right.. but my doubt lies here itself the it will be always true to have previous character occuring 0 or 1 time.. so what's the use of ? then,, I need to know where this ? is useful.. Otherwise we would have use * also, for doing the same thing, as it is returning the same results. – None – 2009-08-15T15:41:13.067

2Yes, a? by itself is nonsensical to search for because it will always match. It only makes sense as part of a larger regex. – chaos – 2009-08-15T15:45:19.003

Can you give any example of such an reg-expression.. where i can know one use of ?.. – None – 2009-08-15T15:46:21.153

He meant a? = "" or "a". In this case everything matches "". You are searching for one or zero characters of one character which would also match anything. – None – 2009-08-15T15:50:30.460

I have just given an example, to convey my question effectively.. But what i need is actual implementation use of "egrep ?". Is there any reg-expr from where i could find the actual use of it.. – None – 2009-08-15T15:53:12.777

Did you read my comment? I gave you one – None – 2009-08-15T16:00:11.733

If you want to match a and aa but not aaa or aaaa etc, you would want the regex aa?. – chaos – 2009-08-15T16:02:09.093

@chaos: No, that's simply wrong. aa? will match a, aa, aaa and aaaa. The ? doesn't limit what comes after it. Please try these things out before posting them. – RichieHindle – 2009-08-15T16:47:46.110

@RichieHindle: Hm, let's see if I can state this in a politic fashion. What you're not recognizing is the difference between a regex matching a sequence and it returning positive for a string containing that sequence. If aa? matched aaaa, then aa?b would match aaaab. – chaos – 2009-08-15T16:54:35.003

@chaos: Ah, OK, understood. For what it's worth, I don't think your comment If you want to match a and aa but not aaa or aaaa etc, you would want the regex aa? makes that clear. – RichieHindle – 2009-08-15T18:13:43.940

@RichieHindle: Yeah, perhaps not. Hopefully this additional commentary will have clarified it. – chaos – 2009-08-15T18:16:11.530

I think the canonical example RB was looking for is "ab?c" will match "ac" or "abc". – Jonathan Hartley – 2012-02-27T14:23:19.600

4

It's a single character search, matching one or zero of the character before it.

Note: you need to escape the ? by using a \ first: \?.

Doldrim

Posted 2009-08-15T15:13:41.740

Reputation:

Why the downvote? :/ – None – 2009-08-15T15:39:24.477

Not my downvote, but I don't think your answer makes any sense. Your regex is i-? but you talk about matching i+j. And if you meant to say i+? then that will still match i++. The ? doesn't limit what comes after the match, so i+? will happily match i++. – RichieHindle – 2009-08-15T16:00:48.957

1

In regex speak, a? means 0 or 1 'a's. So if you search for a string that has 0 or 1 a's in it, you'll get everything. A place where it would be useful is matching positive integers:

/^\+?\d+$/

which plays out as

^: beginning of line
\+: + sign
?: 0 or 1 of previous character
\d: digit
+: one or more of previous character
$: end of line

and would match both +123 and 456

Have a look at regular-expressions.info for more info on using regex's.

John Oxley

Posted 2009-08-15T15:13:41.740

Reputation: 707