Unix Shell - using sed to retrieve the hour in a timestamp

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > using sed to retrieve the hour in a timestamp





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author using sed to retrieve the hour in a timestamp
landre@gmail.com

2006-09-28, 7:27 pm

Greetings.

I have a ksh script that greps the contents of a log file, into an
array, selecting only what I want.

the output to a CSV looks like this:

Jun 1 2005|21:08:34|/dir1/dir2/dir3/dir4/filename.txt

the time stamp (21:08:34) is a value in my array and I want to extract
only the hours from it to use elsewhere. in this case, "21"

my knowledge of "sed" is pretty limited, so I'm not sure how I could
parse

${ARRAY[ARRAYINDEX+3]} to return only "21".

Please let me know if you have any suggestions. I'd also like to use
the same approach to return only the filename. basename doesn't like
the size of the array.


Thanks a lot!!!

Michal Nazarewicz

2006-09-28, 7:27 pm

landre@gmail.com writes:

> Greetings.
>
> I have a ksh script that greps the contents of a log file, into an
> array, selecting only what I want.
>
> the output to a CSV looks like this:
>
> Jun 1 2005|21:08:34|/dir1/dir2/dir3/dir4/filename.txt
>
> the time stamp (21:08:34) is a value in my array and I want to extract
> only the hours from it to use elsewhere. in this case, "21"
>
> my knowledge of "sed" is pretty limited, so I'm not sure how I could
> parse
>
> ${ARRAY[ARRAYINDEX+3]} to return only "21".
>
> Please let me know if you have any suggestions. I'd also like to use
> the same approach to return only the filename. basename doesn't like
> the size of the array.


If you have such line in a variable, say LINE, you can use:

#v+
HOUR="${LINE@*|}"
HOUR="${HOUR%%:*}"
FILENAME="${LINE##*/}"
#v-

If you want a sed command which outputs only hour/file name, you can
use:

#v+
sed -ne 's/^[^|]*|\([0-2][0-9]\).*$/\1/p' # outputs hours
sed -ne 's#^.*/\([^/]*\)$#\1#p' # outputs file names
#v-


--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
LionelAndJen@gmail.com

2006-09-29, 1:52 pm

Hey, that worked !

thanks a lot !!!

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com