On Sat, Nov 17, 2018 at 2:50 AM Werner Koch <w...@gnupg.org> 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;
_______________________________________________
Xlog-discussion mailing list
Xlog-discussion@nongnu.org
https://lists.nongnu.org/mailman/listinfo/xlog-discussion

Reply via email to