The use of ? in grep command.. And practical use

grepregex

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

Best Answer

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

Related Question