tags:

views:

1040

answers:

5

Hello,

I'd like to be able to parse date and times out of a log file. Currently they're in the following format:

"02/Jun/2009:14:38:50" but i'd like to separate them in different columns using something available from a linux command line so that the resulting output looks as follows:

"02/Jun/2009" "14:38:50"

could someone please shed some light as to how this can be done?

Regards

+1  A: 

If that's all that's on each line, maybe:

cat file.txt | sed -e 's/:/" "/'
Chris J
Note that this will (intentionally) replace only the first : with that string. Also, be sure NOT to redirect that output back into the original file, or you will end up with an empty file.
Adam Rosenfield
+2  A: 

Chris's answer is best if there are no lines with other formats. If you need to only affect lines with that specific format, here's something:

cat log | sed -e 's/"\([^:]*\):\([^"]*\)"/"\1" "\2"/'
RichieHindle
This worked perfectly, thanks!
Useless use of `cat`: you can use `sed -e '...' < log`
Jakub Narębski
A: 

sub() function of awk returns 1, if substitution succeeds, so:

cat file.txt | awk 'sub(/:/,"\" \"")'

GNU awk can use gensub() function, so this is a sample of back reference below.

cat file.txt | gawk '$0=gensub(/("[^:]*):([^"]*")/,"\\1\" \"\\2",1)'
Hirofumi Saito
A: 

Pure Bash:

while read line ; do echo ${line/:/\" \"}; done < logfile
fgm
A: 

A biterscript to look for regular expression "&:&:&:&" and separate before and after the first colon. (& means any number of any characters).

var str log
cat "logfile" > $log
while ( { sen -r "^&:&:&:&^" $log } > 0 )
do
    var str datetime, date, time
    stex -r "^&:&:&:&^" $log > $datetime
    stex -p "]^:^" $datetime > $date    # String before the first : is date.
    stex -p "^:^[" $datetime > $time    # String after the first : is time.
    echo $date "\t" $time
done

To try, you can download biterscripting. Google it up, or installation instructions are at http://www.biterscripting.com .