Datafile conversion with AWK !

Tom Christiansen tchrist at convex.COM
Thu Dec 14 22:25:34 AEST 1989


In article <539 at csoftec.csf.com> root at csoftec.csf.com (Cliff Manis (cmanis at csoftec)) writes:
>I am needing help with data conversion, and would appreciate help 
>in AWK or SED and/or awk & sed.   Or whatever....

If you'll accept perl (version 3.0 because I like the array slices), this 
code does it for you, assuming the input files are really as shown:

    #!/usr/bin/perl
    while (<>) {
	chop; split(/\|/);
	if ( $_[$#_] == 1 ) { 
	    &print_line unless $#addrs < 0;
	    @preamble  = @_[0..2];
	    @addrs     = @_[3];
	    @postamble = @_[4..$#_];
	} else {
	    push(@addrs, $_[3]);
	}
    } 
    &print_line;
    sub print_line {
	push(@addrs,'') while $#addrs < 3;
	print join ('|', @preamble, @addrs, @postamble), "\n";
    }

I thought it a bit odd that the input data had a trailing pipe and the
requested output data didn't, but that's what the guy wanted.  If he
really does want a trailing pipe, change the last print line to:
	print join ('|', @preamble, @addrs, @postamble, "\n");

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist at convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"



More information about the Comp.unix.questions mailing list