Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Posts
    4

    Unanswered: Help with Shell Script

    Hi,

    I have three large data files that need alteration. Each consists of -9999 (null data) entries and 5 or 6 digit numeric entries (104625, 99841, etc). I would like to divide the numeric entries by 100000 while allowing the null data entries to remain intact. There is also six rows of "header information" that needs to be skipped over as the alteration is performed.

    A small portion of one file:

    ncols 1484
    nrows 1680
    xllcorner -117.231250
    yllcorner 41.98958333
    cellsize 0.00416667
    nodata_value -9999
    -9999 -9999 -9999 -9999 -9999 -9999 103456 102321 109884 102223
    -9999 -9999 -9999 -9999 -9999 -9999 99667 94557 103445 103224

    and so on...

    i would like to have:

    ncols 1484
    nrows 1680
    xllcorner -117.231250
    yllcorner 41.98958333
    cellsize 0.00416667
    nodata_value -9999
    -9999 -9999 -9999 -9999 -9999 -9999 1.03456 1.02321 1.09884 1.02223
    -9999 -9999 -9999 -9999 -9999 -9999 0.99667 0.94557 1.03445 1.03224

    Thank you in advance for your help!

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi malinut89,
    try this :

    #!/bin/ksh

    I="mydata.org" # your inputfile
    O="mydata.new" # your outputfile
    > $O

    cat $I | awk ' BEGIN { FS=" "; i=0}
    {
    #### maximum values per row
    max = 10

    #### read the nodata_value
    if ( $1 == "nodata_value" )
    {
    ndv = $2
    }
    i=i+1
    #### write the the first six rows as header to new file
    if (i < 7)
    {
    print $0 > "mydata.new"
    }
    else
    {
    x=0
    while (x != max)
    {
    x=x+1
    #### null data entries to remain intact
    if ( $x != ndv )
    {
    $x = $x / 100000
    }
    }
    x=0
    while (x != max)
    {
    x=x+1
    printf ("%s " ,$x) > "mydata.new"
    }
    printf ( "\n") > "mydata.new"

    }
    }'
    Greetings from germany
    Peter F.

  3. #3
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    nawk -f mali.awk myFiles*

    here's mali.awk:
    Code:
    BEGIN {
      _div="100000"
      _nul="-9999"
      PATnum="[-.0-9]"
    }
    
    FNR=1 { out= FILENAME "_new"}
    
    $1 ~ PATnum {
       for(i=1; i <= NF; i++)
         $i = ( $i == _nul ) ? _nul : sprintf("%.5f", $i / _div )
       print >> out
    }
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •