{"id":1823,"date":"2014-01-13T10:38:44","date_gmt":"2014-01-13T18:38:44","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1823"},"modified":"2014-01-15T11:49:58","modified_gmt":"2014-01-15T19:49:58","slug":"convert-date-and-time","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1823","title":{"rendered":"Using grep to convert date and time."},"content":{"rendered":"<p>I was given a flat file that had the date in this format<br \/>\n<pre><code class=\"preserve-code-formatting\">\nDate,Hour,Min,Seconds,Temperature,Unused field\n11\/25\/2013,14,0,0,59.69,\n11\/25\/2013,14,15,0,60.38,\n11\/25\/2013,14,30,0,61.85,\n11\/25\/2013,14,45,0,62.57,\n<\/code><\/pre><br \/>\nbut the table that I need to put it into is formatted as<br \/>\n<pre><code class=\"preserve-code-formatting\">\n(64349,&#039;2014-01-06 20:45:00&#039;,44.258,NULL),\n(64350,&#039;2014-01-06 21:00:00&#039;,44.978,NULL),\n<\/code><\/pre><\/p>\n<p>In BBEdit I used this grep to convert the date and separate out the temp into a separate field.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nFind\n([0-9]+)\/([0-9]+)\/([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9.]+),\nReplace\n&#039;\\3-\\2-\\1 \\4:\\5:\\6&#039;,\\7,NULL),\n<\/code><\/pre><\/p>\n<p>What I\u2019ve done is separate out the different parts of the date with parentheses. Then I rearranged the month, day, year with \\3-\\2-\\1 and added the dash separator. The time is in the correct order so all I need to do is put in the right separator. I want an SQL file when I\u2019m done so I added the &#8216; field separators and the NULL field and line terminator. <\/p>\n<p>I&#8217;ll use the numbering feature of BBEdit to add the row numbers and then add a paren at the beginning of the line. I also need to remember to change the last comma to a semi-colon. Then I can import the data to the MySQL table.<\/p>\n<p>After I rearranged the date, I did need to fix months and days that were only one digit\u2014they need a 0 before the digit. I did this manually with a find and replace. Same with the time. If you did this a lot, you could write a grep that it looked for records with only one digit and prefix it with a zero. A ? will look for exactly one occurrence of a digit. Then do a second pass to get the rest. Or you could use the # to look for one or two digits.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was given a flat file that had the date in this format Date,Hour,Min,Seconds,Temperature,Unused field 11\/25\/2013,14,0,0,59.69, 11\/25\/2013,14,15,0,60.38, 11\/25\/2013,14,30,0,61.85, 11\/25\/2013,14,45,0,62.57, but the table that I need to put it into is formatted as (64349,&#039;2014-01-06 20:45:00&#039;,44.258,NULL), (64350,&#039;2014-01-06 21:00:00&#039;,44.978,NULL), In BBEdit I used this grep to convert the date and separate out the temp into a separate field. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1823\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using grep to convert date and time.<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1823","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1823"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1823\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}