xlog-discussion
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xlog-discussion] A few xlog patches


From: Elwood Downey
Subject: Re: [Xlog-discussion] A few xlog patches
Date: Sat, 17 Nov 2018 12:04:56 -0700



On Sat, Nov 17, 2018 at 2:50 AM Werner Koch <address@hidden> wrote:
Because ADIF is not easy to parse with standard Unix tools. 

I could not resist a challenge like this. Here is a perl script that scans an ADIF file and lists any given columns in CSV format. Enjoy!



#!/usr/bin/perl -w
# print each desired field as a CSV list from each record of the ADIF file on stdin.
# ADIF specification at http://www.adif.org/adif.html
# This software is in the public domain.
# 20181117: first version

use strict;
use warnings;

# print usage unless at least one arg
if (@ARGV < 1) {
    $0 =~ s:.*/::;
    print STDERR "Purpose: list fields from ADIF file records in CSV format\n";
    print STDERR "Usage: $0 field1 field2 ... < adif_file\n";
    exit 1;
}

# local variables
my $saweoh;                                                             # EOH> flag
my @csv;                                                                # columns in one report line

# print column headings
print "# " . join (",", @ARGV) . "\n";

# scan stdin
while (<STDIN>) {
    foreach (split (/</)) {                                             # split at each <
        if (/eoh>/i) { $saweoh = 1; next;}                              # check for EOH
        if (!$saweoh) {next;}                                           # skip until find EOH
        if (/eor>/i) {                                                  # check for EOR
            my $sep = "";                                               # csv separator starts blank
            foreach (@csv) {                                            # print each column
                $_ = "" unless (defined($_));                           # beware missing fields
                print "$sep$_";                                         # print column preceded with sep
                $sep = ",";                                             # now use ,
            }
            print "\n";                                                 # EOL
            @csv = ();                                                  # reset report line
            next;                                                       # next field
        }
        my ($field, $len, $value) = /([^:]+):([\d]+)[^>]*>(.*)/;        # crack
        next if (!$field or !$len or !$value);                          # skip if empty
        my ($column) = grep { $ARGV[$_] eq $field } (0 .. @ARGV-1);     # find desired column for field
        next unless (defined($column) and $column >= 0);                # skip if not wanted
        $csv[$column] = sprintf "%*.*s", $len, $len, $value;            # add at column at spec length
    }
}

# good
exit 0;




 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]