regex question using egrep

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex question using egrep
# 1  
Old 07-18-2011
regex question using egrep

Hi, i have a a bunch of directories that are always named with six lowercase alpha's and either one or two numeric's (but no more)

so for example names could be


I am currently using two pattern matches to capture these names

echo $DIR | egrep '/[a-z][a-z][a-z][a-z][a-z][a-z][1-9]'
echo $DIR | egrep '/[a-z][a-z][a-z][a-z][a-z][a-z][1-9][0-9]/'

so effectively I am having to create two lines to match both scenarios.

i could use

echo $DIR | egrep '/[a-z][a-z][a-z][a-z][a-z][a-z][0-9]+'

but we have specific requirements NOT to match directories with 3 or more numbers at the end, so I cant use this

according to my regex cheat sheet, i should be able to do this ...

echo $DIR | egrep '/[a-z][a-z][a-z][a-z][a-z][a-z][0-9]{1,2}'

but this doesnt work Smilie

any ideas would be greatly appreciated
# 2  
Old 07-18-2011
You can use ? to specify "one or none", but what you really need are anchors:
echo $DIR | egrep '^[a-z]{6}[0-9]{1,2}$'

Because something like
echo $DIR | egrep '[a-z]{6}[0-9]{1,2}'

will match 'qwertyuiio01234' also, since it does contain 6 alphas and 1digit:

Last edited by mirni; 07-18-2011 at 12:45 PM..
# 3  
Old 07-18-2011
this should work...
ls -latr | egrep "[aA-zZ]+([0-9])?[0-9]$"

# 4  
Old 07-18-2011
Considering your spec you should be aware that:
~/$ echo abc-123| egrep '[a-z]{3}-[0-9]{1,2}'

~/$ echo abc-123| egrep '^[a-z]{3}-[0-9]{1,2}$'

And which grep are you running?
~/$ egrep --version
egrep (GNU grep) 2.5.1

# 5  
Old 07-18-2011
ok,I am testing this for use in sudo and basically made the foolish assumption that because sudo works for things like


that it would work for richer regexes ... well it doesnt .. it seems sudo's regex support is rudimentary at best

I got your recommendations woorking from the command line using egrep though, so thanks for all your help

just gutted that my sudo rules have to look so ugly Smilie
# 6  
Old 07-18-2011
Originally Posted by shamrock
this should work...
ls -latr | egrep "[aA-zZ]+([0-9])?[0-9]$"

That bracket expression is quite odd and almost certainly incorrect. The a and Z are each included twice, once within the range expression and once without. Since range expressions are undefined outside of the POSIX locale, it's safe to assume that this is intended to run in that locale. A-z in the POSIX locale, aside from including all of the upper case and lower case letters in the English alphabet, also includes a few other characters: <left-square-bracket>, <backslash>, <right-square-bracket>, <circumflex>, <underscore>, <grave-accent>.

Why are there six characters located between the upper and lower case alphabets? They pad the beginning of the lowercase alphabet so that it's exactly 32 positions above the beginning of the uppercase alphabet. Simply flipping a single bit is then sufficient to convert between upper and lower case.

POSIX locale collation sequence:

I suggest sticking with [A-Za-z] if a POSIX range expression is desired or the [[:alpha:]] character class for any locale.

This User Gave Thanks to alister For This Post:
# 7  
Old 07-18-2011
ok,I am testing this for use in sudo and basically made the foolish assumption that because sudo works for things like


that it would work for richer regexes ... well it doesnt .. it seems sudo's regex support is rudimentary at best
What does sudo have to do with regular expressions? It is the tool, egrep, that processes the regexps, and unless you have some weird setup that egrep for root user is different than for a regular user, than it shouldn't make a difference.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Lines of code in egrep question

Hi, I have a question, during my readings it appears that these two variables in the snippet below need to be on the same line to return a “true” answer and listed in the output otherwise it won’t be returned. How can I write this snippet to make it return a “true” if those two variables are on... (6 Replies)
Discussion started by: bdby
6 Replies

2. Shell Programming and Scripting

sed and egrep question

Its really 2 questions, but both are pretty basic. Linux Redhat 1. Need to do a search and replace on a file. I need to append '--' (comment out the line) to specific lines based on a wildcard search. So if I Have GRANT SOME_ROLE_OR_USER ... I dont care what comes after that.... (2 Replies)
Discussion started by: guessingo
2 Replies

3. Shell Programming and Scripting

Regex for HP egrep

I seem to be having an issue with an egrep command and I think its the way it is interpreting my regex. I have a test file.. ###FIND THESE### telnet /telnet `telnet -l` ###Dont FIND THESE### donotfindtelnet telnetdontfind Working Regex on Linux/Solaris egrep '\<(telnet|rcp)\>' $file ... (4 Replies)
Discussion started by: nitrobass24
4 Replies

4. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

5. Shell Programming and Scripting

Regex/egrep matching numbers in brackets

Experts: I don't know that regular expressions will ever be easy for me, so if one of you guru's could help out, I'd appreciate it. I'm trying to match a line in our syslog, but I can't figure out how to match a number inside a bracket. This is what I'm trying to match. "Jul 16 00:01:34... (2 Replies)
Discussion started by: jdveencamp
2 Replies

6. UNIX for Dummies Questions & Answers

egrep count question

Hello all, I'm a first time poster and a unix/linux noob so please be understanding. I am trying this command below: # egrep -c "Oct".+"Connect: ppp" /var/log/messages* /var/log/messages:53 /var/log/messages.1:35 /var/log/messages.2:63 /var/log/messages.3:27 /var/log/messages.4:12 ... (1 Reply)
Discussion started by: morrowtech
1 Replies

7. Shell Programming and Scripting

egrep regex

Hi, I have a file with some words divided into syllables by the character "|" (pipe). For example zu|ri|ghe|se.I would like a regex that matches all the words that are not divided in syllables.All the word that have no "|" pipe character.I have thought at $echo "zu|ri|ghe|se" | grep '' ... (7 Replies)
Discussion started by: and77
7 Replies

8. Shell Programming and Scripting

egrep question

I want to egrep for certain fields which are not existing in the current log files and am getting errors for that... egrep "'^20090220.14'|'^20090220.15'|'^20090220.16'|'^20090220.17'|'^20090220.18'" Some of the times are in future and logs don't have those entries and I get errors for them... (1 Reply)
Discussion started by: jacki
1 Replies

9. UNIX for Dummies Questions & Answers

Egrep question

I have a script that does the following. It searches a listing of directories with specific extensions and then formats a wc on those files. The code looks like this find <directory> -name '*.js' -o -name '*.html' | awk '{print \"wc -l \"$1}' > file \n" The result is a file with the "wc -l"... (7 Replies)
Discussion started by: mastachef
7 Replies

10. UNIX for Dummies Questions & Answers

question on egrep

Hi I am trying to use this command: egrep '^a{2,6}$' testexpr4D to retreive lines with 2,3,4,5, or 6 a's in a file . The file testexpr4D has entries like: a aa aaa aaaa aaaaa aaaaaa 123456 ABCDEF I was expecting to see 5 lines in the output but nothing happens. Can anyone help... (10 Replies)
Discussion started by: rohitv
10 Replies
Login or Register to Ask a Question