ned help with re grep

ned help with re grep

Post by Ric » Sat, 11 May 2002 05:54:39



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?

Rick

 
 
 

ned help with re grep

Post by Chris F.A. Johnso » Sat, 11 May 2002 06:02:52



> 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?

    Sounds like a job for awk:

        awk -F: '$3 < 10000' /etc/passwd

--

        =================================================================



 
 
 

ned help with re grep

Post by Barry Margoli » Sat, 11 May 2002 06:03:25




>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?

awk -F: '$2 < 10000 { print }'

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

ned help with re grep

Post by Michael Heimin » Sat, 11 May 2002 06:05:14



Quote:> 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?

> Rick

Would use awk on this one, like Ie.:

$ awk -F: '$3>10 && $3<30{print $1":"$3}' /etc/passwd
at:25
puh:26
lnx:27
mdom:28
yard:29
man:13

Michael Heiming
--
Remove the +SIGNS case mail bounces.

 
 
 

ned help with re grep

Post by laura fairhe » Sat, 11 May 2002 06:30:40



Quote:>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?

The natural way to do this is to use 'awk' because (i) it can automatically
determine the fields based on a given deliminator (ii) it can do a
comparison like '<' just by putting the expression.

awk '$2<10000' FS=: test.txt

FS is the field deliminator which if set to a single character
acts just like the 'cut' deliminator does. A pattern with no action
defaults to printing out the current record if true so {print} is not
needed on the end of the comparison.

A good reference for 'awk' by the original authors is available
on my website here;

http://lf.8k.com/UNIX/UNIX.HTM

It is possible (just) to do the comparison/field-cutting by formulating a
couple of basic regular expressions (or one big extended one) although
when you want to change the condition the whole RE would have to be
changed. For example to test for all numbers up to but not including
10000 you could use;

grep '^[^:]*:[0-9]\{1,4\}$' test.txt

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

Quote:

>Rick

byefrom

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492

 
 
 

ned help with re grep

Post by Ric » Sat, 11 May 2002 11:00:21


Thanks a ton everyone.


> >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?

> The natural way to do this is to use 'awk' because (i) it can automatically
> determine the fields based on a given deliminator (ii) it can do a
> comparison like '<' just by putting the expression.

> awk '$2<10000' FS=: test.txt

> FS is the field deliminator which if set to a single character
> acts just like the 'cut' deliminator does. A pattern with no action
> defaults to printing out the current record if true so {print} is not
> needed on the end of the comparison.

> A good reference for 'awk' by the original authors is available
> on my website here;

> http://lf.8k.com/UNIX/UNIX.HTM

> It is possible (just) to do the comparison/field-cutting by formulating a
> couple of basic regular expressions (or one big extended one) although
> when you want to change the condition the whole RE would have to be
> changed. For example to test for all numbers up to but not including
> 10000 you could use;

> grep '^[^:]*:[0-9]\{1,4\}$' test.txt

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

> >Rick

> byefrom

 
 
 

ned help with re grep

Post by laura fairhe » Sun, 12 May 2002 10:51:07




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

 
 
 

1. grep sTerm * | grep -v grep ???

in .bashrc

alias r='ps ax | grep $1 | grep -v grep'

this works from the command line but doesn't work from the bashrc.
Apparently there's a syntax problem.

...and

. filename

where can I read about this commad that is found in bash and sh
scripts?  I searched the bash and one or two other man pgs to no
avail.  Thanks gurus.

--


2. Help! Start/stop inetd without reboot?

3. tail | grep | grep | grep

4. RARPD ERROR

5. Ned help in installing Linux,...

6. Latest 'pppd to NT server' files

7. ned help on unix

8. Adding New Disk on 2.6 (Ultra AXi Motherboard)

9. ned help installing dmsdos

10. No passed on Linux System, ned help

11. PLEASE HELP A NEWBIE: grep doesn't grep

12. C.C.Li [magnoptic.ned@casema.nl]

13. I ned a Netcard and Serialcard