How to extract decimal value from following

I have following values in a file

Time:       3.610 [ms] (mean)
Time:       1.805 [ms] (mean, across all concurrent requests)

I need the decimal value in first row 3.610 for which I amd using following regex but the regex returns value in 2nd row 1.805 instead

while (<FILE>) {
            if ($_ =~ /Time:\s*(\d+\.\d+)\s*\S+/)      {
                    $time= $1;
                    print "\ntime: $time\n";
            }

Can someone please help me with this ?

Answers


It might be printing both values but the ques is how to extract only 1st value

You didn't state that in your original question. As others have mentioned, using last

while (<FILE>) {
    if ($_ =~ /Time:\s*(\d+\.\d+)\s*\S+/) {
       $time = $1;
       print "\ntime: $time\n";
       last;    #This will leave the "while" loop as soon as it find a match.
    }
}

You can also put all of the entries into an array, and access any one you want that way:

my @time_list;
while (<FILE>) {
    if ($_ =~ /Time:\s*(\d+\.\d+)\s*\S+/) {
       $time = $1;
       print "\ntime: $time\n";
       push @time_list, $time;
    }
}

print "First time is $time_list[0]\n";

Need Your Help

Application hanging on customer machine

c# debugging .net-4.0 cpu-usage hang

My .NET 4.0 Application is hanging on a customer Machine. The same customer has a Windows 7 where it hangs and a Windows XP where it is working very fast with no hangers. Now, I see, that on the Wi...

Big Theta Proof

complexity-theory big-o analysis

I got a practice exam question here asking if the following is true/false.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.