in reply to Re: Multiply the numbers in a text file
in thread Multiply the numbers in a text file

Thanks for the reply.

my file looks something like this

ncols 498 nrows 541 xllcorner 1394682.720294049475 yllcorner 1751328.700356452260 cellsize 2000.000000000000 NODATA_value -9999 -9999 -9999 -9999 -9999 -9999 -9999 0.09 -0.001

Though it is a lot bigger in terms of the number of rows and columns. So what i want to do is skip the first six lines and multiply the rest of the text excluding -9999 with a constant. Also all the small negative values should be converted to 0 or -9999. The resulting can be overwritten onto the same text file or onto a new file.

Thanks a lot for your help

Replies are listed 'Best First'.
Re^3: Multiply the numbers in a text file
by aaron_baugher (Curate) on May 10, 2015 at 06:34 UTC

    Now that you've provided some sample data, we can accomplish something. You still need to make your requirements more concrete, though. What qualifies as a "small negative value"? All negative values, or ones below a certain value? If these should be converted to "0 or -9999", which should it choose? If some should be 0 and some should be -9999, based on what criteria? If you hired me to write this script, I would need you to answer those questions.

    To get you started, you can skip the first six lines by simply reading and writing them:

    for (1..6){ $line = <$input_file>; print $output_file $line; }

    Then proceed with your filtering on the remaining lines. I'll guess that you want all negative values smaller than -0.1 replaced with 0 or -9999 alternatively, assume that values are space-delimited, and continue:

    my $M = 5; # constant multiplier my $T = 0.1 # negative number threshold my $al = 0; # alternator while(<$input_file>){ s|([-.\d]+)| if($1 == -9999){ $1; # leave -9999 alone } elsif( $1 < 0 and abs($1) < $T ){ $al++ % 2 ? 0 : -9999; # replace small negative with 0 or -999 +9 } else { $1 * $M; # multiply other numbers by constant } |ge; print $output_file; }

    If you want your changes to replace the original file, the best way to do that is to write to a new file and then copy it over the old file when you're finished. You can do that copy manually, or make that part of your script after you've tested enough to be confident that it will work correctly.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      Any value less than zero will be considered as a negative number except -9999, because that represents the no data value.

      Thanks for your help Aaron. I'm trying to get the code running

        After trying out your code, I get the output which I'm unable to interpret

        OB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)GLOB(0xc828e8)

        It looks something like this

        It doesn't write in the output file except for the first six lines. Can you tell me what the error is?