Ajinkya
Ajinkya

Reputation: 83

How to split the date and time from log file?

I want to display the date and time separated with space. The test.log file contains lots of other information as well.

grep -E '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}' test.log

test.log

2016-03-02T10:23:16
2016-03-02T10:23:16
2016-03-02T10:23:16
2016-03-02T10:23:16
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:23:36
2016-03-02T10:24:35

Output should be

2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

Upvotes: 0

Views: 412

Answers (3)

riteshtch
riteshtch

Reputation: 8769

$ awk -F 'T' '{print $1, $2}' test.log 
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

or

$ sed 's/T/ /' test.log 
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

or

$ tr "T" " " < test.log 
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

Edit:

Since you have fields separated by space in a single line ,take the 1st field and remove brackets to get the date and time like this:

$ awk '{gsub(/^[\s\[]*|[\s*\]]*$/, "", $1); print $1}' <<< "[2016-02-24T19:59:41.584+00:00] [userId: <anonymous>] [ecid: 005B8a1_^kLDoYUO26L6hz0002S700031R,0:3] [APP: clarientloginga] [DSID: 0000LCKpi5C7y0a_lLK6yZ1MmtvP00006z] [SRC_CLASS: com.dtcc.iam.login.servlet.PostAuthnServlet] [SRC_METHOD: doGet] RETURN" | tr 'T' ' '
2016-02-24 19:59:41.584+00:00

Upvotes: 1

Benjamin W.
Benjamin W.

Reputation: 52152

You can use cut with T as the input delimiter and define a space as the output delimiter:

$ cut -d T --output-delimiter=' ' -f 1,2 test.log
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

Upvotes: 0

anubhava
anubhava

Reputation: 785196

Simple tr command will do the job:

tr 'T' ' ' < file

2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:16
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:23:36
2016-03-02 10:24:35

Upvotes: 0

Related Questions