The R Project SVN R

Rev

Rev 2160 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! @PERL@
# Convert fixed width format to R/S "table"
# By KH <Kurt.Hornik@ci.tuwien.ac.at>, based on a script by Doug Bates
# Usage:  fwf2table -f FORMAT [-s SEP] FILE,
# where FORMAT is the format string, typically "Ax Ay Az ..." with x, y,
# z etc being the widths of the columns and `A' means that the field is
# to be interpreted as an ASCII character string with trailing white
# space suppressed, and SEP is the record separator to be inserted in
# the output (default is a space).
#require "getopts.pl";

require 5.000;
use Getopt::Std;

getopts("f:s:");
die "A format string must be specified.\n" unless $opt_f;
$sep = $opt_s ? $opt_s : " ";
while (<>) {
    undef @rec;
    foreach (unpack($opt_f, $_)) {
    s/^\s+//;
    if (/\s/) {
        $_ = "\"" . $_ . "\"";
    }
    elsif (/^$/) {
        $_ = "NA";
    }
    push(@rec, $_);
    }
    print(join($sep, @rec), "\n");
}