Perl for comparing numbers from previous lines in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl for comparing numbers from previous lines in a file?
# 1  
Old 11-21-2013
Perl for comparing numbers from previous lines in a file?

Hi everyone

I have a question for you, as I am trying to learn more about Perl and work with some weather data. I have an ascii file (shown below) that has 10 lines with different columns. What I would like is have Perl find an "anomalous" value by comparing a field with the values from the last 3 lines (for the same field, of course). For instance, if we look at the data below:
Code:
A15 26.62 765  27.30 4.3
A11 26.63 763  27.28 4.2
A12 26.68 767  27.29 4.3
A16 26.64 768  27.30 4.2
A11 26.62 761  27.31 4.1
A15 26.62 765  27.30 4.3
A15 26.63 763  27.28 4.2
A16 26.68 767  2.29 4.3
A17 26.64 768  27.30 4.2
A18 26.62 761  27.31 4.1

We see here that on the 8th line, the value in column 4 is "off" with respect to the previous lines (all of which are approx. 27.3 degrees Celsius).

Thus, how can I do to compare the values for a line in column 4 with the values from the previous 3 lines in order to detect these "bad" data values? Assume that any value that dirfts +/- than 0.5deg C is "bad data"

I am not sure how to proceed. Do I need to load the data into an array? Do I need to read the file line by line? Or maybe something else?

Any help, suggestions, and especially examples would be really helpful.

Thanks in advance Smilie

Sorry... I forgot to mention that this is what I have in my script so far:

Code:
my $file = "$WORK_DIR/tmpdata.asc";

open my $info, $file or die "Could not open $file: $!";
my %line;
local @ARGV = ($file);

while ( <$info> ) {
        my $x1 = $_;
        my @cols = split(" ", $x1);

        print "@cols\n";

}

close $info;


Last edited by Scott; 11-21-2013 at 11:17 PM.. Reason: Code tags for data too
# 2  
Old 11-22-2013
How about:

Code:
my $file = "$WORK_DIR/tmpdata.asc";

open my $info, $file or die "Could not open $file: $!";
my %line;
local @ARGV = ($file);
my @temps;

while ( <$info> ) {
        my $x1 = $_;
        my @cols = split(" ", $x1);

        if(@temps &&
            abs(@cols[3] - eval(join("+", @temps)) / @temps) > 0.5) {
            print "@cols\n"
        } else {
            shift(@temps) if (@temps > 3);
            push(@temps, @cols[3]);
        }
}

close $info;

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-22-2013
This works great... thanks Chubler!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two files with numbers and taking difference in third file

Hi All, I have two files in the following format, with numbers being defined under columns(described by a set of headers) and rows(again defined by a set of identifiers) 2013 2013 Make200 Make201 Merc BMW Jpur Del ... (9 Replies)
Discussion started by: dev.devil.1983
9 Replies

2. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

3. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

grep lines from the previous day's file

I have files called printfile.log.1121, printfile.log.1122, etc where 1121 and 1122 are the date of the file creation. The file contents are like: ================================================================================ User = d_prod Env = d_prod Dir =... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

5. Shell Programming and Scripting

ksh comparing current and previous lines

Hi, I am currently trying to work out how to compare one line with the last line I have read in via ksh. I have a file which has sorted output from a previous sort command so all the lines are in order already and the file would look something like show below. Each line has a name and a time... (5 Replies)
Discussion started by: paulie
5 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

comparing lines in file

i have 2 files and i want to compare i currently cat the files and awk print $1, $2 and doing if file1=file2 then fail, else exit 0 what i want to do is compare values, with column 1 being a reference i want to compare line by line and then still be able to do if then statement to see if worked... (1 Reply)
Discussion started by: sigh2010
1 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. Shell Programming and Scripting

Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###. I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block if matched, need to print that whole block. According to the input file in below, it will print... (5 Replies)
Discussion started by: tqlam
5 Replies

10. UNIX for Dummies Questions & Answers

comparing numbers in a file

Hello, I'm searching for a quick method to read numeric values from a file or a defined variable and identifying the largest number. For instance if the following numbers are in a file or defined to a variable: 09192007 09202007 09182007 09172007 09162007 What "short" method could be used to... (7 Replies)
Discussion started by: dusk2dawn
7 Replies
Login or Register to Ask a Question