JDR
JDR

Reputation: 33

how to split lines in unix log file output

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

Upvotes: 0

Views: 3683

Answers (5)

P M
P M

Reputation:

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 .

Upvotes: 0

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17208

Pure Bash:

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

Upvotes: 1

Hirofumi Saito
Hirofumi Saito

Reputation: 321

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)'

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281825

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"/'

Upvotes: 3

Chris J
Chris J

Reputation: 1375

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

cat file.txt | sed -e 's/:/" "/'

Upvotes: 1

Related Questions