[nycbug-talk] Regular expressions

Trish Lynch trish at bsdunix.net
Tue Nov 6 11:12:17 EST 2007


At that point - usuing awk is much lighter than calling perl - just one of those pet peeves I have is people calling perl from shell scripts when the tools have been provided (sed and awk) to call from shell that are just as powerful.

Usuing the same "methodology" piping it to sed would lso work to edit the output and replace it with just the number.

-Trish
-- 
Trish Lynch
M: 646-401-1405
H: 201-378-0434  

-----Original Message-----
From: Brian McGonigle <brian.mcgonigle at gmail.com>

Date: Tue, 6 Nov 2007 10:50:31 
To:talk at lists.nycbug.org
Subject: Re: [nycbug-talk] Regular expressions


On Nov 6, 2007, at 1:23 AM, 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.
>
> 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.
>
>

Pipe it to " perl -pe 's/.*length: (\d+)\).*/$1/g;' " and you'll get
just the number following "length: " and before the closing ")".
It essentially does a find and replace on the input.

".*length: (\d+)\.*" 		matches the entire input string. The (\d+)
matches one or more digits  following "length: " and stores it in $1.
Then it replaces the input with the just the digits following "length:
" and prints them.


_______________________________________________
talk mailing list
talk at lists.nycbug.org
http://lists.nycbug.org/mailman/listinfo/talk


More information about the talk mailing list