- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- quick SED and AKW questions
Operating System - Linux
1753873
Members
7819
Online
108809
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2006 06:29 AM
тАО12-22-2006 06:29 AM
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
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
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2006 07:13 AM
тАО12-22-2006 07:13 AM
Solution
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.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-22-2006 09:18 AM
тАО12-22-2006 09:18 AM
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...
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 10:26 AM
тАО01-08-2007 10:26 AM
Re: quick SED and AKW questions
The last perl script was just what I need. The results are at www.appsremote.com/metar . Thanks!
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP