Re: using sed to retrieve the hour in a timestamp



landre@xxxxxxxxx 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--
.



Relevant Pages