Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from stackoverflow.com Aug 15 '09 at 16:31

This question came from our site for professional and enthusiast programmers.

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).

share|improve this answer
    
I understood it.. Thanks a lot.. Also what does 'egrep a? filename' will do actually.. If you can put it in words.. – RB Aug 15 '09 at 16:28
3  
@RB: egrep a? filename will match every line. It's not useful on its own. – RichieHindle Aug 15 '09 at 16:48
    
Regular GNU grep (without the -E) supports the ? but you must escape it. echo "color" | grep "colou\?r" Should return color. – Matt Kneiser Jan 21 '14 at 19:53

? 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.

share|improve this answer
    
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. – RB Aug 15 '09 at 15:41
2  
Yes, a? by itself is nonsensical to search for because it will always match. It only makes sense as part of a larger regex. – chaos Aug 15 '09 at 15:45
    
Can you give any example of such an reg-expression.. where i can know one use of ?.. – RB Aug 15 '09 at 15:46
    
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. – Doldrim Aug 15 '09 at 15:50
    
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.. – RB Aug 15 '09 at 15:53

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: \?.

share|improve this answer
    
Why the downvote? :/ – Doldrim Aug 15 '09 at 15:39
    
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 Aug 15 '09 at 16:00
    
Thanks for this clarification. – Doldrim Aug 15 '09 at 16:05

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .