More fun with sed

March 18, 2011

(This article was first published on Greg's Research » R, and kindly contributed to R-bloggers)

So I have this strange date and time string, which I would like to convert to a “useable” date, i.e., something that a spreadsheet programme or R can work with. It looks like this (MON has 3 chars):


The string is the second field in a csv file, preceded and followed by a comma.

My strategy was to terminate the string before the first colon and delete everything thereafter to be left with the following string (with one occurrence in each of the about 6000 lines of the file):

sed does this in a single line (looks kinda ugly, but does the trick):
sed 's/:[0-9][0-9]:[0-9][0-9]:[0-9][0-9]//g' myfile.csv >myfile2.csv

To leave a comment for the author, please follow the link and comment on their blog: Greg's Research » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)