Reputation: 13
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
Upvotes: 1
Views: 145
Reputation: 10039
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, ... )
Upvotes: 0
Reputation: 41456
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 ,
Upvotes: 1
Reputation: 174706
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.\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. Upvotes: 2