Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression 1

Status
Not open for further replies.

keziah

Programmer
Dec 20, 2002
10
FR
Hello,

How can i have a regular expression for :
8 digit and one caractere (q) or one caractere only (q)

I tried this ^[0-9]\{8\}q\{0,1\} but it's for 8 digit and
one or none letter.

Thanks
 
an egrep solution:

egrep "^([0-9]{8})?q" your_file

-jim

 
Thanks for answer but Oupss ...

I'm sorry, i forget to mention that is for sed used.
With egrep it's sure, it's work fine but with sed :

sed 's/^\([0-9]\{8\}\)\?q\{1\}$//' or
sed 's/^\([0-9]\{8\}\)\{1\}q\{1\}$//'

does not work ...

Can you do it with sed ?
happy.gif


Regards,
 
This is untested but might do what you want.

sed 's/[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}[0-9]\{0,1\}q//' file CaKiwi
 
Thanks but this not work because you can have
one or two or three or ... heigth digit with this
expression ...

And i want 8 digit or none ...

Regards
 

Post examples of the input data and the results you want.

Or maybe use J1mbo's regular expression with awk

awk '{sub(/^([0-9]{8})?q/,"");print}' filename CaKiwi
 
Here is a sed way to match either
- 8 digits then 'q'
- 'q'

sed -n '/^[0-9]\{8\}q/p ; /^q/p'

-jim
 
Thanks Jim

It's works great.
So with sed there is no way to do:

"this regular expression one time or more" ?
 
There is a way to do this with sed. Let's say your RE was [a-z]. If you wanted to find 1 or more occurances of this on a line, then you would use something like:

sed -n '/[a-z][a-z]*/p' file

This basically says show me one occurance of a-z then zero or more occurances of a-z. Sed does not have extended regular expressions like egrep or awk where you can use the parenthesis to group a regular expression. The parenthesis in sed are used for alteration, which remembers the characters the RE matched. Sed also does not support the "+" sign to mean 1 or more. Sorry for the delayed response,

-jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top