[Thread Prev][Thread Next][Index]

Re: [ferret_users] Reading ASCII data including formatted dates



Ferret users,

On Tue, Mar 20, 2018 at 8:07 AM, William S. Kessler <william.s.kessler@xxxxxxxx> wrote:
[ . . . ]
I finally kluged this by editing the text

I'd like to add something that might be useful to somebody in the future.  I sometimes have to edit text files to use as input to Ferret or other tools and do these things a lot.  In this particular case, we want convert "05-Sep-2007" to "05/09/2007".  Using the GNU version of the date command, this is how you do that:

date --date="05-Sep-2007" +"%d/%m/%Y"

(On Mac, you can install it by "brew install coreutils" and the name of the command will be "gdate" instead of "date".)  The 3rd parameter looks scary but it's nothing but a format string like Fortran's "F10.3", etc.

The next piece is to read lines from the text file, modify it, and write the result to a new file:

cat inputfile.txt | while read col1 col2 rest; do
    newdate=$(date --date="$col2" +"%d/%m/%Y")
    echo $col1 $newdate $rest
done > modified.txt

But, you need to skip the header lines.  So you skip the first two lines:

tail +2 inputfile.txt | while . . . .

In this way, you have a shell script that converts your text file.  If you need to process many files in the same way, you want to pass the names of the input and output files as parameters to the shell script:

tail +2 "$1" | . . .
    . . . .
done  > "$2"

where $1 and $2 are the 1st and 2nd parameters to the script.

You can use it from the command line to preprocess your input files, or you can call it from within Ferret:

yes? SPAWN ./convertdata.sh input.txt modified.txt
yes? FILE/. . . modified.txt

Cheers,
Ryo

[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement