0

I have a large file which contains data in specific format. I want to convert date format with different one.

from: YYYY-MM-DD hh:mm:ss.sss

To: YYYY-MM-DDThh:mm:ss.sss000+00:00

I can print out the desired format using awk

awk -F " " '{print substr($1,0,4)"-"substr($1,6,2)"-"substr($1,9,2)"T"substr($2,1,2)":"substr($2,4,2)":"substr($2,7,6)"000+00:00"}' my-file.txt

but struggling to replace it inline. Also have heard that sed has similar capability to do inline replacement.

I have tried running below sed command but its not working -

sed 's/\([0-9]{4}-[0-9]{2}-[0-9]{2}\s\) \([0-9]{2}:[0-9]{2}:[0-9]{2}\s\) \1/T\2 /g'

It says unterminateds' command`

Any suggestion/pointers are appreciated.

03ce007
  • 51
  • 1
  • 5
  • 2
    Could you please post few samples in your post and let us know then? – RavinderSingh13 Feb 21 '19 at 12:27
  • Have you read the regex info in the `sed` documentation? It can do what you want. And what do you mean by replacing "inline"? – lurker Feb 21 '19 at 12:28
  • See https://stackoverflow.com/questions/34071098/how-to-change-date-format-in-sed and https://stackoverflow.com/questions/12696125/sed-edit-file-in-place – Wiktor Stribiżew Feb 21 '19 at 12:29
  • @RavinderSingh13 I am doing `sed -i 's/\([0-9]{4}-*[0-9]{2}-[0-9]{2}\s) \([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}\s) \1T\2 /g' my-file.txt` but getting `unterminated `s' command` error. – 03ce007 Feb 21 '19 at 12:45
  • Your substitute command is incorrect. The end should be "/\1T\2 /g" instead of "\1/T\2 /g". – Alain Merigot Feb 21 '19 at 14:39
  • @AlainMerigot Thank you. I've got it running, but not updating the content of file! `sed -i.bak 's/([[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}) ([[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}.[[:digit:]]{3})/\1T\2000+00:00/g' my-file.txt` – 03ce007 Feb 21 '19 at 14:51
  • Show some real example data and your expected output please. – Til Feb 21 '19 at 15:32

0 Answers0