Ajinkya Ajinkya - 6 months ago 12
Bash Question

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

Answer
$ 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
Comments