extracting YYYY-MM-DD from string

extracting YYYY-MM-DD from string

Post by OtisUsen » Wed, 09 Jan 2002 05:04:14



Hello,

I have file names (log files) with a time stamp in their name.
Here is an example:

  log.2002-01-07-235959.gz

I'd like to use sed to extract YYYY, MM, and DD portion.
So far I was only able to get rid of the .gz part using something like this:

  filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

I checked the man page, but couldn't figure it out from there.

Can anyone help?

Thanks,
Otis

 
 
 

extracting YYYY-MM-DD from string

Post by Michael Heimin » Wed, 09 Jan 2002 05:30:43




January 2002 21:04:

Quote:> Hello,

> I have file names (log files) with a time stamp in their name.
> Here is an example:

>   log.2002-01-07-235959.gz

> I'd like to use sed to extract YYYY, MM, and DD portion.
> So far I was only able to get rid of the .gz part using something
> like this:

>   filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

> I checked the man page, but couldn't figure it out from there.

> Can anyone help?

> Thanks,
> Otis

One out of x solutions, (using awk):

echo <filename> | awk -F- '{sub("log.","");print $1,$2,$3}'

2002 01 07

Michael Heiming
Use nawk/gawk if you are on Solaris...

 
 
 

extracting YYYY-MM-DD from string

Post by Ben.Altma » Wed, 09 Jan 2002 05:48:27



> Hello,

> I have file names (log files) with a time stamp in their name.
> Here is an example:

>   log.2002-01-07-235959.gz

> I'd like to use sed to extract YYYY, MM, and DD portion.
> So far I was only able to get rid of the .gz part using something like this:

>   filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

> I checked the man page, but couldn't figure it out from there.

> Can anyone help?

I wouldn't use sed, but in anycase:
    echo log.2002-01-07-235959.gz | sed 's/log.\(.*\)-.*.gz/\1/'

Alternatively:
    logfile=log.2002-01-07-235959.gz
    newfile=${logfile#log.}
    newfile=${newfile%-*}

Or for individual fields (ksh):
    echo $logfile | IFS=. read _ a _
    echo $a | IFS=- read YYYY MM DD _

regards,
Ben

 
 
 

extracting YYYY-MM-DD from string

Post by Dan Merc » Wed, 09 Jan 2002 05:41:59




> Hello,

> I have file names (log files) with a time stamp in their name.
> Here is an example:

>   log.2002-01-07-235959.gz

> I'd like to use sed to extract YYYY, MM, and DD portion.
> So far I was only able to get rid of the .gz part using something like this:

>   filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

> I checked the man page, but couldn't figure it out from there.

> Can anyone help?

> Thanks,
> Otis

in ksh:

for i in log.*.gz
   do
   print $i | IFS=".-" read log year month day sz gz
   print "$i logged on $month/$day/$year"
   done

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

extracting YYYY-MM-DD from string

Post by Joe Halpi » Wed, 09 Jan 2002 06:09:17





> January 2002 21:04:
> > I have file names (log files) with a time stamp in their name.
> > Here is an example:

> >   log.2002-01-07-235959.gz

> > I'd like to use sed to extract YYYY, MM, and DD portion.
> > So far I was only able to get rid of the .gz part using something
> > like this:
> One out of x solutions, (using awk):

> echo <filename> | awk -F- '{sub("log.","");print $1,$2,$3}'
> 2002 01 07

Another way with shell builtins:

#!/bin/ksh
IFS=".-"
filename="log.2002-01-07-235959.gz"
set -- $filename
echo $2 $3 $4

Joe

 
 
 

extracting YYYY-MM-DD from string

Post by Trevor Osatch » Wed, 09 Jan 2002 06:33:43




Quote:>Hello,

>I have file names (log files) with a time stamp in their name.
>Here is an example:

>  log.2002-01-07-235959.gz

>I'd like to use sed to extract YYYY, MM, and DD portion.
>So far I was only able to get rid of the .gz part using something like this:

>  filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

If you want to get rid of all of the numbers and the .gz so that you
only end up with 'log' you can:

filename=`echo log.2002-01-07-235959.gz | awk -F. '{ print $1 }'`

This will make $filename = .

This prints off the first field of the name of the file with the field
separator as "."(period)

Hope that's what you wanted.

Quote:

>I checked the man page, but couldn't figure it out from there.

>Can anyone help?

>Thanks,
>Otis

Trevor Osatchuk

Any man whose errors take ten years to correct is quite a man.
          - J. Robert Oppenheimer, speaking of Albert Einstein

 
 
 

extracting YYYY-MM-DD from string

Post by Trevor Osatch » Wed, 09 Jan 2002 07:13:17






>>Hello,

>>I have file names (log files) with a time stamp in their name.
>>Here is an example:

>>  log.2002-01-07-235959.gz

>>I'd like to use sed to extract YYYY, MM, and DD portion.
>>So far I was only able to get rid of the .gz part using something like this:

>>  filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

>If you want to get rid of all of the numbers and the .gz so that you
>only end up with 'log' you can:

>filename=`echo log.2002-01-07-235959.gz | awk -F. '{ print $1 }'`

>This will make $filename = .

Of course, I meant $filename = log

- Show quoted text -

Quote:

>This prints off the first field of the name of the file with the field
>separator as "."(period)

>Hope that's what you wanted.

>>I checked the man page, but couldn't figure it out from there.

>>Can anyone help?

>>Thanks,
>>Otis

>Trevor Osatchuk

>Any man whose errors take ten years to correct is quite a man.
>          - J. Robert Oppenheimer, speaking of Albert Einstein

Trevor Osatchuk

Any man whose errors take ten years to correct is quite a man.
          - J. Robert Oppenheimer, speaking of Albert Einstein

 
 
 

extracting YYYY-MM-DD from string

Post by Dave Granti » Wed, 09 Jan 2002 13:05:58




Quote:> Hello,

> I have file names (log files) with a time stamp in their name.
> Here is an example:

>   log.2002-01-07-235959.gz

> I'd like to use sed to extract YYYY, MM, and DD portion.
> So far I was only able to get rid of the .gz part using something like this:

>   filename=`echo log.2002-01-07-235959.gz | sed 's/\.gz//'`

Alrighty, some options in sed:
sed 's/log\.\([^-]*\)-\([^-]*\)-\([^-]*\).*/\1 \2 \3/'
sed -e 's/log.//' -e 's/-[^-]*.gz//' -e 's/-/ /g'

cheers
Dave

+-----------------------+-----------------+-----------+


+-----------------------+-----------------+-----------+

 
 
 

1. Using sed to convert YYYYMMDD to YYYY-MM-DD

I use an application which generates .xml files containing <date> lines
formatted like:

<date>20050911</date>

What I'd like to do is make them more readable by adding dashes like:

<date>2005-09-11</date>

That should be a one-liner in sed, but I'm new to it and can't make it work.
Anyone know how?

The only additional factor I have is that occasionally there are also <date>
lines like this, which I want ignored (of course):

<date>1997</date>

All lines that aren't <date> lines should also be ignored.

Thanks for any help

2. How to disable keyboard

3. Date-time conversion from "YYYY-MM-DD HH:MI:SS" to unix epoch

4. SGI admin job offered

5. How to convert Unix date to DD/MM/YYYY HH:MM:SS

6. HELP! Netscape Web Server SSI!

7. how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

8. What windows manager is best for newbies?

9. how can i convert a mm/dd/yyyy uu:mm:ss to a time_t ?

10. ksh script: convert mm/dd/yyyy to julian??

11. Changing all date fields from mm/dd/yy to dd/mm/yy

12. Files modified since yy/mm/dd hh:mm

13. dd command: extract single files out of a dd-file