Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

quick SED and AKW questions

SOLVED
Go to solution
Alan_152
Honored Contributor

quick SED and AKW questions

I am working with a string that looks like this:

KAPA 221753Z 16010KT 10SM FEW140 SCT200 M01/M11 A2997 RMK AO2 SLP183 4/018 T10061106 10006 21089 51014

I would like to manipulate this file a bit:

1) I want to delete "RMK" and everything after that to the right.

2) I would like to create a 5 column file out of the remaining objects. Unfortunately, of the remaining data, only the 1st 3 columns (station ID, day of month and time, and wind direction and speed) in the original line are ALWAYS in that sequence. The last 2 colums (temperature and dewpoint, and altimiter setting) are always before RMK in that order. However, the stuff between the wind speed and the temperature is variable in length, format, and number of columns. What I would like the line to look like is this:

KAPA 221753Z 16010KT M01/M11 A2997

3) I'd like to further modify things so that it looks like this:

KAPA 22 17:53 160 10 -01 -11 29.97

(for the temp and dewpoint, M in the original file is -, and P in the original file is +).

BTW, this isn't homework for me -- it is more of an extracurricular research project for me, mixing two of my 3 main loves (computers and weather). I just haven't mastered sed and awk quite well enough to do the job.

Thanks
Alan Sheets



3 REPLIES
Stuart Browne
Honored Contributor
Solution

Re: quick SED and AKW questions

Personally, I'd use perl:

perl -n -e '/^(.*)RMK/ and { print $1. "\n" }'

That's the first one.

The third, something like:

perl -n -e '/^(\w+) \d{2}(\d{2})(\d{2}). (\d{3})\d+\w+ .*([MP])(\d+)\/([MP])(\d+) \w(\d+) RMK/ and printf("%s %d:%d %s %s%02d %s%02d %0.2f\n", $1, $2, $3, $4, ($5 == "M" ? "-" : "+"), $6, ($7 == "M" ? "-" : "+"), $8, $9 / 100)'

It's mostly just pattern matching, and manipulation. Pretty simple stuff.

I'm sure you can work backwards to get the second.

One long-haired git at your service...
James R. Ferguson
Acclaimed Contributor

Re: quick SED and AKW questions

Hi Alan:

This seems to fit your requirements (using Perl):

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my $line;
while (<>) {
( $line = $_ ) =~ s/(.+)RMK.+/$1/;
my @fields = split /\s+/, $line;
next unless $#fields >= 5;
my ( $staid, $montm, $winds, $tmpdp, $altm )
= ( $fields[0], $fields[1], $fields[2], $fields[-2], $fields[-1] );
printf "%s %2s %2s:%2s %3s %2s %s%2s %s%2s %2s.%2s\n", $staid,
substr( $montm, 0, 2 ), substr( $montm, 2, 2 ),
substr( $montm, 4, 2 ), substr( $winds, 0, 3 ),
substr( $winds, 3, 2 ), substr( $tmpdp, 0, 1 ) eq "M" ? "-" : "+",
substr( $tmpdp, 1, 2 ), substr( $tmpdp, 4, 1 ) eq "M" ? "-" : "+",
substr( $tmpdp, 5, 2 ), substr( $altm, 1, 2 ), substr( $altm, 3, 2 );
}
1;

...run as:

# ./filter filename

Regards!

...JRF...
Alan_152
Honored Contributor

Re: quick SED and AKW questions

The last perl script was just what I need. The results are at www.appsremote.com/metar . Thanks!