>>I need to pull values from a text file that are under a value. An
>>example is grabbing those users with user ID's under 10000 from the
>>/etc/passwd.
>>I cut out the username and ID so my file looks like
>>USERNAME:15025
>>USERNAME2:155505
>>I have been trying to use
>>grep ^'[a-z]*' ':' '[0-9]{1,5}' test.txt
>>Can anyone please point me in the right direction for this?
[]
Quote:>It is important to have anchor '$' at the end otherwise a 5 digit
>number would still be matched okay.
>If you wanted to look for all numbers up to 12000 it would
>get more complicated;
>egrep '^[^:]*:([0-9]{1,4}|1[0-1][0-9]{3})$' test.txt
>If you really had to do it with an RE I would use a shell
>script to generate the RE first, but unless there's a good reason it's
>just an interesting exercise...
I was bored silly <g> so I wrote an awk script to convert ARG[1] into
an ERE that matches all values i s.t 0<=i<=ARG[1]. The ERE just
matches the digits so you need to add an anchor to the beginning
and end and because of precendence (anchors get glued to alternation
terms) you need brackets as well, eg;
re=`awk -f rang.awk 1199`; egrep '^[^:]*:('"$re"')$' filename
Probably interesting is how far the ERE could be optimised.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~rang.awk
function repstr(str,count)
{
ts=""
for(tt=0;tt<count;++tt)ts=ts""str
return ts
Quote:}
function re_rang(x)
{
# create regular expression to match all values i s.t 0<=i<=x
# not optimal!
# (not optimal: successive 9s on the end of a value should be collapsed
# to 1 term and also terms could be factorized )
s=""
n=split(x,a,"")
t=n
if(10**t-1>x)t--
if(t==1)s="[0-9]"
if(t==2)s="[1-9]?[0-9]"
if(t>2)s="([1-9]"repstr("[0-9]?",t-2)")?[0-9]"
if(t==n) return s
p=""
for(i=1;i<=n;++i){
at0=0;at1=a[i]-1
if(i==n)at1++
else if(i==1)at0=1
if(at1>=at0){
if(at0==at1)at=at0
else at="["at0"-"at1"]"
if(s!="")s=s"|"
s=s p at repstr("[0-9]",n-i)
}
p=p""a[i]
}
return s
Quote:}
BEGIN{
print ""re_rang(ARGV[1])
exit
Quote:}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~rang.awk
buenasnoches
--
# if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492