transposing data {awk?}

Dave Lawrence tale at cs.rpi.edu
Wed Dec 6 18:09:06 AEST 1989


In article <4473 at netcom.UUCP>, beaulieu at netcom (Bob Beaulieu) writes:
> Is it possible to use awk to change the 7th field of "YYMMDD" to
> show "MM/DD/YY" by using the awk command?

In article <5281 at omepd.UUCP> merlyn at iwarp.intel.com (Randal Schwartz) writes:
> Here's an awk one-liner:
>  awk '{ $7 = substr($7,3,2) "/" substr($7,5,2) "/" substr($7,1,2); print }'
> Here's the equivalent in Perl (of course)...
>  perl -ane '$F[6] =~ s#(..)(..)(..)#$2/$3/$1#; print join(" ", at F),"\n";'

In  <LIMES.89Dec5180251 at ouroborous.wseng.sun.com> limes at sun.com (Greg Limes):
>   How about a SED one-liner?
>           sed 's;\(..\)\(..\)\(..\);\2/\3/\1;'
>   Ya don't need a jackhammer when a jacknife will do ;-)

Only if the knife _will_do.  This won't.  It just transforms the first
occurance of six characters on a line.  The original request, as you
cited, asked for field seven of the record.  So, in sed:

$ sed "s#\([ \t]*[^ \t]+[ \t]*[^ \t]+[ \t]*[^ \t]+[ \t]*[^ \t]+[ \t]*[^ \t]+[ \t]*[^ \t]+[ \t]*\)\(..\)\(..\)\(..\)#\1\3/\4/\2#"

(Substitute hard tabs for \t if your sed doesn't comprehend that as TAB.)

Yeah, sed will do the job.  Sure is ugly though.

Dave
-- 
   (setq mail '("tale at cs.rpi.edu" "tale at ai.mit.edu" "tale at rpitsmts.bitnet"))



More information about the Comp.unix.questions mailing list