2
use strict;
use warnings;

my $file = 'tagged.txt';
open(my $info, '<', 'tagged.txt') or die $!;

while(my $line = <$info>){
    if ($line =~ /someString/){
        #
    } else if ($line =~ /someOtherString/){
        #
    }
    #...
}

close $info

So I'm trying to parse some wild log files into a well formatted .csv file. I want to look through the logfile line by line and check if one of my desired values / strings appear in that line. Let's say we have something like this:

Aug 27 13:59:36 topas user.info POS[548]: 00:11:33.989304 <GPS> Input To Sensor Fusion - f64Longitude (deg) = 13286667

One of the values I'm interested in is for example the f64Longitude (deg), respectively the value that appears behind "f64Longitude (deg) = ". My question is now: how do I access the string that is located after some regEx that I've found...

3
  • Your question is not very clear, post proper input data and output what are you expecting. Commented Apr 27, 2015 at 13:01
  • $1 - see stackoverflow.com/questions/7855637/… You may need to expand your search to include the portion after the f64... You should consider yourself lucky if that is always the terminating portion before \n Commented Apr 27, 2015 at 13:08
  • This may be tangential, but you r'else if' shou'd be 'elsif' Commented Apr 27, 2015 at 14:39

1 Answer 1

2

Just specify the token you are looking for and use regex groups to retrieve matching part. For example:

my $str = 'Aug 27 13:59:36 topas user.info POS[548]: 00:11:33.989304 <GPS> Input To Sensor Fusion - f64Longitude (deg) = 13286667';
if ($str =~ /f64Longitude \(deg\) = (\d+)/) {
    print $1;
}

#prints 13286667
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.