How to convert "Nov 9 11:35:28 2009" to "200911091135"

Using AIX 5.3 and /user/bin/ksh.

Anyone have a quick way to convert the string date

Nov 9 11:35:28 2009 to 200911091135

I know I could create a table of months and find the month number by searching the list, but I was hoping there was some handy little known command to do this type of date conversion to save me the trouble.

Thanks,
Troy

$
$ date -d "Nov 9 11:35:28 2009" +%Y%m%d%H%M
200911091135
$
$ date -d "Apr 1 9:5:7 2009" +%Y%m%d%H%M
200904010905
$

tyler_durden

:frowning:

/>date -d "Nov 9 11:35:28 2009" +%Y%m%d%H%M
date: illegal option -- d
Usage: date [-u] [+Field Descriptors]

:frowning:

what about man date ?

I have the same problem...

date -d ###### date: illegal option -- d

I'm on bash....on snow leopard

man date give this.

     -d dst  Set the kernel's value for daylight saving time.  If dst is non-
             zero, future calls to gettimeofday(2) will return a non-zero for
             tz_dsttime.

Can't help with my version of GNU date :frowning:

       -d, --date=STRING
              display time described by STRING, not �now'

Well if you have Perl, then:

$
$ d="Nov 9 11:35:28 2009"
$
$ echo $d | perl -MDate::Parse -ne '@x=split; @y=strptime("$x[1] $x[0] $x[3] $x[2]");
                                    printf("%d%02d%02d%02d%02d\n",(1900+$y[5]),($y[4]+1),$y[3],$y[2],$y[1])'
200911091135
$
$ d="Apr 1 9:5:7 2009"
$
$ echo $d | perl -MDate::Parse -ne '@x=split; @y=strptime("$x[1] $x[0] $x[3] $x[2]");
                                    printf("%d%02d%02d%02d%02d\n",(1900+$y[5]),($y[4]+1),$y[3],$y[2],$y[1])'
200904010905
$
$

tyler_durden

Thank you durden_tyler.

It's works but i don't understand perl.....i go to study this...

Indeed i want to compare file in a directory and a list of file in a file text for extract the newer of the same file if there are a same file....

Thanx.

Another one with awk:

echo "Nov 9 11:35:28 2009"|
awk -F" |:" '
BEGIN {
  a="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  split(a,m,",")
  for(i=1;i<13;i++)
    n[m]=i
}
{printf("%s%02d%02d%s%s\n", $6, n[$1], $2, $3, $4)}
'

i thank that exist a simple solution with the command date...but not with my version.
Thank you to Franklin.

echo "Nov 9 11:35:28 2009" | awk 'BEGIN{
  m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|")
  for(o=1;o<=m;o++){
     date[d[o]]=sprintf("%02d",o)
   }     
}
{
  n=split($3,a,":")
  print $NF date[$1] sprintf("%02d",$2) a[1] a[2]
}' 

output

# ./shell.sh
200911091135

Hi.

With RedHat:

date -d "Nov 9 11:35:28 2009" '+%Y%m%d%H%M'

When trying to use the Perl code:

BEGIN failed--compilation aborted.
Can't locate Date/Parse.pm in @INC (@INC contains: /qdxtest/qdx5.4/integrator/lib/perl5/5.6.0/aix /qdxtest/qdx5.4/integrator/lib/perl5/5.6.0/aix /qdxtest/qdx5.4/integrator/lib/perl5/5.6.0 /qdxtest/qdx5.4/integrator/lib/perl5/site_perl/5.6.0/aix /qdxtest/qdx5.4/integrator/lib/perl5/site_perl/5.6.0/aix /qdxtest/qdx5.4/integrator/lib/perl5/site_perl/5.6.0 /qdxtest/qdx5.4/integrator/lib/perl5/site_perl/5.6.0/aix /qdxtest/qdx5.4/integrator/lib/perl5/site_perl/5.6.0 /qdxtest/qdx5.4/integrator/lib/perl5/site_perl . /work/jerickso/quovadx_dev/qdx5.4P/integrator/lib/perl5/5.6.0/aix /work/jerickso/quovadx_dev/qdx5.4P/integrator/lib/perl5/5.6.0 /work/jerickso/quovadx_dev/qdx5.4P/integrator/lib/perl5/site_perl/5.6.0/aix /work/jerickso/quovadx_dev/qdx5.4P/integrator/lib/perl5/site_perl/5.6.0 /work/jerickso/quovadx_dev/qdx5.4P/integrator/lib/perl5/site_perl .).
BEGIN failed--compilation aborted.

Another awk solution:

echo "Nov 9 11:35:28 2009" | awk -F '[ \t:]' '
{
   printf("%04d%02d%02d%s%s\n", $6, (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3, $2, $3, $4)
}'

Jean-Pierre.

Had to download Date::Parse and Time::Zone from CPAN and then the perl worked like a charm. Thx all!

Anyone have a suggestion on where I can bulk download all the most popular Modules for Perl?

If you are asking about the location from where you can download, then the answer is CPAN, but you knew that already.

As for "most popular modules", there isn't such a list really. It depends on what you want to do. CPAN has modules for probably anything worthwhile you'd want to do with Perl - file processing, text processing, databases, xml, interfacing with other languages/webservers etc., graphics, email, i18n etc.
I have DBI and a bunch of db driver modules for working with databases.

Before installing a module, you may want to read the reviews on CPAN. And also go through the methods list, to see if they solve the problem you have.

HTH,
tyler_durden