Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

(cLive ;-)(Golf) Analyzing Time From tcpdump Output

by cLive ;-) (Prior)
on Feb 05, 2002 at 03:06 UTC ( [id://143341]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Analyzing Time From tcpdump Output

42
123456789 2 3 4 % cat data.output | perl -p0e'/(\d+\.\d+).*(\d+\.\d+)/s;$_=$2-$1.$:'
I added a closing CR because output doesn't display otherwise (at least not for my install :) - without it, it's 39

cLive ;-)

update - as chipmunk points out, this fails. In my effort to fp a reply, I missed out the : , ie:

% cat data.output | perl -p0e'/(\d+\.\d+).*:(\d+\.\d+)/s;$_=$2-$1.$:'
Shame on y'all who ++d my reply :)

Replies are listed 'Best First'.
Re: (cLive ;-)(Golf) Analyzing Time From tcpdump Output
by chipmunk (Parson) on Feb 05, 2002 at 03:43 UTC
    I'm afraid your solution has a bug; the greedy matching of .* means that $2 only contains one of the digits before the decimal point, rather than both. Here's another way to match: perl -lp0e'/([\d.]{9}).*([\d.]{9})/s;$_=$2-$1' Using -l is shorter than using $:, and avoids outputting a spurious '-' after the newline. :)
      Are you sure? Try running it... :)

      The \d+ is greedy too...

      cLive ;-)

        Yes, I am sure. I did run it, and it output the wrong answer:
        -49.994325 -
        It doesn't matter that \d+ is greedy, because .* is greedy and gets to go first. The .* only backtracks far enough to allow the \d+ to match. \d+ can match a single digit, so .* only gives up a single digit. But to get the correct answer, \d+ has to match two digits before the decimal point, not one.

        On the other hand, it's okay if $2 only contains one digit before the decimal point, if $1 also contains a single digit before the decimal point: perl -lp0e'/(\d\.\d+).*(\d\.\d+)/s;$_=$2-$1'

Re: (cLive ;-)(Golf) Analyzing Time From tcpdump Output
by petral (Curate) on Feb 05, 2002 at 15:38 UTC
    Or
    perl -lp0e's/\S*:(\S+).*:..:(\S+).*/$2-$1/es' data.output
    TIMTOWTDI:
    perl -lpe'$b=(split":")[2];$a||=$b}{$_=$b-$a' data.output

      p

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://143341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-16 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found