Separate date and time with a comma

147 views Asked by At

I have access log with lines

http://***.com ,**.**.**.**,2013-06-07 12:03:58 ,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0. 

I need the date and time to be separated by a comma using sed

3

There are 3 answers

0
Avinash Raj On BEST ANSWER

Seems like you want something like this,

$ sed 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\) \([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)/\1,\2/g' file
http://***.com ,**.**.**.**,2013-06-07,12:03:58 ,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0. 
  • \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\) Capture the date string.
  • Match the in-between space character.
  • \([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\) Capture the time string.
  • Then replace the matched strings with \1 characters inside group index 1, , \2 characters inside group index 2.
  • \(\) called capturing group in Basic regular expressions. So for example \([0-9]\{4\}\) would capture the 4 digit number into a group.
2
Jotne On

This is much more simple to do with awk

awk -F, '{sub(/ /,",",$3)}1' OFS=, file
http://***.com ,**.**.**.**,2013-06-07,12:03:58 ,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0.

Separate the files using ,, replace first space in 3rd field with ,

0
NeronLeVelu On

A bit secure in a complicated sed

sed 's/\(,[-0-9]\{10\}\) *\([0-9:]\{8\},\)/\1,\2/g' YourFile

A bit easy in a very complicated sed vs a simple awk

sed 's/  */,/' YourFile

but in both case (as earlier reply post) it assumes that there is only line like the sample in the file.If not you have to give the other possible file (as a http log , any error taht can occure like bad URL message, ... )