[nycbug-talk] Regular expressions

Marc Spitzer mspitzer at gmail.com
Tue Nov 6 02:20:25 EST 2007


On 11/6/07, jonathan at kc8onw.net <jonathan at kc8onw.net> wrote:
> Hello all,
>
> I see to remember there being a way to specify that part of the condition
> you used to create the match is not returned when the regex returns.  I'm
> trying to return just the number from the "length: 40" portion of this
> line.
>
> "IP (tos 0x0, ttl  47, id 52246, offset 0, flags [none], proto: TCP (6),
> length: 40)"
>
> I'm currently using  grep -Eo "length: ([[:digit:]]{2,4})" but that
> includes "length: " in the output.

What grep are you using, as far as I know grep should return the whole
line that matches the pattern.  What you want is awk,perl,ruby,tcl or
some such thing.

now if the length field is the last field you could do this

awk '{print $NF}' file

to print out "40)" the pipe to tr like so, yes I am being lazy and
wasting processes.
awk '{print $NF}' file |tr -d ')'

and you should get out your number.

have fun,

marc


>
> If someone can give me the right term or concept to search for I'll do the
> legwork I'm just out of ideas on what to search for and didn't see
> anything relevant in the man pages.
>
> Thanks,
> Jonathan Stewart
>
> P.S. There is probably a better way but the main goal of this is to
> monitor bandwidth usage by IP on a pflog device.  I'll probably do most of
> the work in Python I'm just prototyping in the shell right now.
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nycbug.org
> http://lists.nycbug.org/mailman/listinfo/talk
>


-- 
Freedom is nothing but a chance to be better.
Albert Camus



More information about the talk mailing list