Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex for breaking up one column unix file into three columns... 2

Status
Not open for further replies.

qyllr

MIS
Mar 8, 2001
131
US
hello,

i'm trying to break up a unix file with the following sample data into three columns:

16:42:33.02
16:42:36.37
16:42:46.50
16:42:49.83
16:43:00.08
16:43:03.79

the original file contained other data, but i was mainly interested with these times...so i initially passed through an array the original file and wrote regex to match the strings with the times and finally produced the data from above...i ultimately need to do the following:

start finish difference

16:42:33.02 16:42:36.37 (diff between start and finish)
16:42:46.50 16:42:49.83
16:43:00.08 16:43:03.79

can i write regex for this or do i need to do a print format...can i also calculate the difference between the start and finish times?

any suggestions will be greatly appreciated...

ty,

q.
 
Well, this is one of those "it's 3:30am and I can't sleep" pieces of code, so it's probably not as efficient as it could be, but it works. Hope this helps you. Moreover, I hope the code is clear enough so you can see what's happening. Moreover, I hope the view window on Tek-Tips doesn't completely mess up the formatting. ;-)

Code:
#!/usr/bin/perl
use POSIX;
use strict;

sub fetch(@);

open(FH,@ARGV[0]);
fetch(<FH>);
close(FH);


sub fetch(@) {
    my($x,$xs,$y,$ys,$d,$ds);
    my(@a);
    format STDOUT_TOP =
Start          Finish           Difference
-------------------------------------------
.
    format STDOUT =
@<<<<<<<<<<<   @<<<<<<<<<<<<   @|||||||||||
$x             $y              $d
.
    while(@_) {
        ($x) = shift =~ /[\w\d\:\.]+/g;
        ($y) = shift =~ /[\w\d\:\.]+/g;
        ($xs) = $x =~ /\.\d{2}$/g;
        ($ys) = $y =~ /\.\d{2}$/g;
        my @xz = split /:/,$x;
        my @yz = split /:/,$y;
        $d = abs((mktime($yz[2],$yz[1],$yz[0],0,0,70) + $ys) - (mktime($xz[2],$xz[1],$xz[0],0,0,70) + $xs));
        $d = sprintf(&quot;%.2f&quot;,$d).&quot; s&quot;;
        write();
    }
}

This will return:
Code:
Start          Finish           Difference
-------------------------------------------
16:42:33.02    16:42:36.37        3.35 s
16:42:46.50    16:42:49.83        3.33 s
16:43:00.08    16:43:03.79        3.71 s

For the values you posted.

Hope this helps,

brendanc@icehouse.net
 
thanks sophisticate...this helped a lot...

q.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top