Omer Levy
Omer Levy

Reputation: 51

Extract specific columns from line and match against given range

I'm trying to identify numbers(float) within a certain range from text using regex, the text format is: 3 1 EDR 918771665157 ^[[32m97.01424 ^[[0m918489349209 ^[[32m97.01408 ^[[0m 3 2 EDR 918493379596 ^[[32m87.01418 ^[[0m918775654999 ^[[32m87.01434 ^[[0m

The ^[[32m and ^[[0m strings are because of colors. I'm trying to check if the 5th and 7th columns are between 93.00000 and 99.99999, however if the value is 93.00000 it might be shown as 93. I tried the following:9[3-9](\.\d{1,5})?\s+\S+\s+.*9[3-9](\.\d{1,5})? but i have a problem since in the second line here the 5th and 7th columns are not in range but i get true because of the 4th and 6th columns

Upvotes: 1

Views: 348

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21666

I suggest you to split the line so that you can get the exact columns and then do desired processing on those columns. See the below approach:

#!/usr/bin/env perl
use strict;
use warnings;

while(<DATA>){
    chomp;
    my @columns = split/\s+/, $_;
    my $fifth_column = $columns[4];
    my $seventh_column = $columns[6];

    #Remove ^[[32m and ^[[0m
    $fifth_column =~ s/\^\[\[32m|\^\[\[0m//g;
    $seventh_column =~ s/\^\[\[32m|\^\[\[0m//g;

    #Verify that above are between 93.00000 and 99.99999
}

__DATA__
3  1  EDR  918771665157  ^[[32m97.01424  ^[[0m918489349209  ^[[32m97.01408   ^[[0m
3  2  EDR  918493379596  ^[[32m87.01418  ^[[0m918775654999  ^[[32m87.01434   ^[[0m

Upvotes: 3

Related Questions