[comp.unix.questions] transposing data {awk?}

beaulieu@netcom.UUCP (Bob Beaulieu) (11/23/89)

I am in the process of t
transferring data from a CPM to unix environ and nee to swap a data field to a
different format. Is it possible to use awk to change the 7th field of "YYMMDD" to show "MM/DD/YY" by using the awk command?

Thanks for any help!

Bob B.
r
-- 
Bob Beaulieu
277-b Tyrella Avenue
Mountain View, CA 94043
(415) 967-4678

tale@pawl.rpi.edu (David C Lawrence) (11/24/89)

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

Sure; in fact, it's trivial.  Please try and get a hold of any basic
AWK reference manual; even the crummy SunOS manual page for awk makes
mention of the features you need.

You'll probably end up with something like this:

awk '{ $7 = sprintf("%s/%s/%s",substr($7,2,2),substr($7,4,2),substr($7,0,2));\
       print; }'

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

merlyn@iwarp.intel.com (Randal Schwartz) (11/29/89)

In article <4473@netcom.UUCP>, beaulieu@netcom (Bob Beaulieu) writes:
| I am in the process of t
| transferring data from a CPM to unix environ and nee to swap a data field to a
| different format. Is it possible to use awk to change the 7th field of "YYMMDD" to show "MM/DD/YY" by using the awk command?

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(" ",@F),"\n";'

Okay, so Perl was only a few chars shorter this time. :-)

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

limes@sun.com (Greg Limes) (12/06/89)

In article <4473@netcom.UUCP>, beaulieu@netcom (Bob Beaulieu) writes:
[ I am in the process of transferring data from a CPM to unix
[ environ and need to swap a data field to a different format. 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@omepd.UUCP> merlyn@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(" ",@F),"\n";'
> Okay, so Perl was only a few chars shorter this time. :-)
> Just another Perl hacker,
> Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095

How about a SED one-liner?

	sed 's;\(..\)\(..\)\(..\);\2/\3/\1;'

Ya don't need a jackhammer when a jacknife will do ;-)

-- Greg Limes [limes@eng.sun.com]
   Just Another UNIX Hacker
--
-- Greg Limes	limes@eng.sun.com	...!sun!limes	73327,2473	[choose one]

tale@cs.rpi.edu (Dave Lawrence) (12/06/89)

In article <4473@netcom.UUCP>, beaulieu@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@omepd.UUCP> merlyn@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(" ",@F),"\n";'

In  <LIMES.89Dec5180251@ouroborous.wseng.sun.com> limes@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@cs.rpi.edu" "tale@ai.mit.edu" "tale@rpitsmts.bitnet"))