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

link|improve this question

0% accept rate
Should this be on Super User instead? – Scott Dorman Aug 15 '09 at 15:16
What Super User Sir.. I just wanted to understand the practical idea being 'grep ?' command in unix.. – RB Aug 15 '09 at 15:18
This site is for programming questions, SuperUser.com is for general IT questions, which this falls under. – Lasse V. Karlsen Aug 15 '09 at 15:25
4  
grep is a pretty heavily used programmer tool. I think there is some grey area on whether or not that's allowed, am i right? – Doldrim Aug 15 '09 at 15:29
feedback

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

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

4 Answers

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

link|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
feedback

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

link|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
feedback

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

link|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
show 7 more comments
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.