1833758 Members
2618 Online
110063 Solutions
New Discussion

Re: regular expression

 
SOLVED
Go to solution
andi_1
Frequent Advisor

regular expression

Hello,

My expression can two forms:
vgcreate -p yara -e yara

or

vgcreate -g yara -p yara -e yara

What kind of regular expression I can use to identify that -g option was used?

I tried "grep vgcreate*.*-g*.*" didn't work!

Thanks a lot!


7 REPLIES 7
Rodney Hills
Honored Contributor

Re: regular expression

Be sure quotes are around the expression-

vgcreate -g yara -p yara -e yara

grep -- "-g " file

The two -- are so grep knows that -g is not on option for it to use.

In your sample you didn't have double quotes around the expression and the shell will attempt to expand them to filenames.

-- Rod Hills
There be dragons...
andi_1
Frequent Advisor

Re: regular expression

Hi Rodney,

I tried you way, didn't work.

grep -- vgcreate*.*"-g"*.* filename

Thanks!
MANOJ SRIVASTAVA
Honored Contributor
Solution

Re: regular expression

Hi Andi

You are right it take "-g" as an option for grep .

Try this


grep "\-g"


this should be good to go.


Manoj Srivastava
MANOJ SRIVASTAVA
Honored Contributor

Re: regular expression

Hi Andi


ie again

grep "\-g" file


Manoj Srivastava
S.K. Chan
Honored Contributor

Re: regular expression

This should work for you ..

# grep -e 'vgcreate* '\-g' *' filename
Sachin Patel
Honored Contributor

Re: regular expression

If it is at same position everytime then you can use cut

#t=1234567890
#echo $t |cut -c 6 will return 6.

In your case it is 11


Sachin
Is photography a hobby or another way to spend $
Charles Stepp
New Member

Re: regular expression

> My expression can two forms:
> vgcreate -p yara -e yara

> or

> vgcreate -g yara -p yara -e yara

> What kind of regular expression I can use to > identify that -g option was used?

> I tried "grep vgcreate*.*-g*.*" didn't work!

> Thanks a lot!

grep 'vgcreate.*-g'

The '*' after the 'e' in vgcreate is a common
confusion between regular expressions and glob
patterns (such as what the shell uses to match
files). Those quotes are single quotes, not backticks; that is IMPORTANT. Double quotes would also work here. The '*' after the 'g'
actually BREAKS the search, since it indicates
that the 0 or more more 'g's can match. It is not necessary to match the entire line to get
grep to match, so the attempt to match
"everything else" after the '-g' is unnecessary.
Keep It Simple Sir