用perl解析纬度和经度

时间:2014-10-22 19:30:24

标签: perl parsing

我这里有一份文件如下:


FICN10 CWIS 211446
ICEBERG BULLETIN FOR THE EAST COAST WATERS ISSUED BY ENVIRONMENT
CANADA AT 11:00 A.M. EDT TUESDAY 21 OCTOBER 2014 FOR TODAY.
THE NEXT SCHEDULED BULLETIN WILL BE ISSUED AT 11:00 A.M. WEDNESDAY.
ICEBERG LIMIT AT 0000 UTC 22 OCT ESTIMATED FROM NEWFOUNDLAND NEAR
4900N 5350W TO 4830N 4900W TO 5000N 4900W TO 6000N 5830W TO
6000N 4000W TO 3000N 5000W.

我正在尝试使用perl脚本,在其中我可以将坐标转换为如下所示的csv文件:

4900N 5350W 
4830N 4900W
5000N 4900W
6000N 5830W
6000N 4000W
3000N 5000W

我有一段代码,其中我试图这样做,因为我没有得到预期的结果:

您能看一下代码,看看应该做些什么才能将文本文件中的所有坐标转换成上述格式?

以下是代码:

`rm -f coords.TXT`;
open FPOUT, ">$outfile" or die "\nERROR: Could create/write $outfile\n\n";
for ( $ii = 0; $ii < 7; $ii++ ) {
    #if ($ii==5)
    #{
    #   print STDOUT "LINE 1\n";
    #}
    #if ($ii==6)
    #{
    #   print STDOUT "LINE 2\n";
    #}
    @words = split " ", "$inline[$ii]";
    for ( $jj = 0; $jj < (@words); $jj++ ) {
        @char = split "", "$words[$jj]";
        $n = 0;
        for ( $kk = 0; $kk < (@char); $kk++ ) {
            $n = $n + 1;
        }
        if ( $n =~ 5 or $n =~ 6 ) {
            if ( $char[0] =~ /[a-zA-z]/ ) {

            } else {
                if ( $char[4] =~ "W" or $char[4] =~ "E" or $char[4] =~ "N" or $char[4] =~ "S" ) {
                    if ( $n =~ 6 ) {
                        if ( $char[5] =~ "." ) {
                            $words[$jj] =~ s/\.//;
                        }
                    }
                    if ( $char[4] =~ "W" or $char[4] =~ "E" ) {
                        $lon = $words[$jj];
                        print STDOUT "longitude is $lon\n";
                    } elsif ( $char[4] =~ "N" or $char[4] =~ "S" ) {
                        $lat = $words[$jj];
                        print STDOUT "Latitude is $lat\n";

                    }
                    print FPOUT "$lat $lon\n";
                }

            }
        }

    }
}

close FPOUT;

1 个答案:

答案 0 :(得分:3)

使用正则表达式解析数据

要提取所需的模式,您只需要一个正则表达式:

while (<DATA>) {
    print "$_\n" for /\b\d+[NS]\s+\d+[WE]\b/g;
}

__DATA__
FICN10 CWIS 211446
ICEBERG BULLETIN FOR THE EAST COAST WATERS ISSUED BY ENVIRONMENT
CANADA AT 11:00 A.M. EDT TUESDAY 21 OCTOBER 2014 FOR TODAY.
THE NEXT SCHEDULED BULLETIN WILL BE ISSUED AT 11:00 A.M. WEDNESDAY.
ICEBERG LIMIT AT 0000 UTC 22 OCT ESTIMATED FROM NEWFOUNDLAND NEAR
4900N 5350W TO 4830N 4900W TO 5000N 4900W TO 6000N 5830W TO
6000N 4000W TO 3000N 5000W.

输出:

4900N 5350W
4830N 4900W
5000N 4900W
6000N 5830W
6000N 4000W
3000N 5000W

Mark Setchell

的文件处理

有关如何使用Perl处理文件的一些示例,我建议您查看:perlfaq5 - How do I change, delete, or insert a line in a file, or append to the beginning of a file?

在您的具体情况下,可能会使用以下内容:

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;

my $file = 'bulletin.txt';

open my $fh, "<", $file;

while (<$fh>) {
    print "$_\n" for /\b\d+[NS]\s+\d+[WE]\b/g;
}