27182818
27182818

Reputation: 91

How to locate and convert the all date format for a file.txt?

Suppose I have a diary.txt file. I import it as a string. All the dates in this string appear to be YYYY.MM.DD, and I want to locate and convert it into DDMMYYYY. What should I do?

For example, here is a diary.txt,

2018.01.01
It's a nice day.

2018.01.02
Today is a rainy day.

It should be converted into

Jan 01 2018
It's a nice day.

Jan 02 2018
Today is a rainy day.

Upvotes: 0

Views: 382

Answers (2)

Roman Luštrik
Roman Luštrik

Reputation: 70643

You first need to coerce your dates to proper date objects (as.Date) and then replace them with a newly formatted date. See ?strptime for the syntax on how to specify the new format.

# import data
diary <- tempfile(fileext = ".txt")
cat("2018.01.01
It's a nice day.

2018.01.02
Today is a rainy day.", file = diary)

xy <- readLines(con = diary)

# coerce to proper date format
dates <- as.Date(xy, format = "%Y.%m.%d")

# replace valid dates with new dates formatted using format()
# date should be all non-NAs
xy[!is.na(dates)] <- format(dates[!is.na(dates)], format = "%b %d %Y") # %b will depend on your locale, see ?strptime

# write to file
writeLines(xy, con = "result.txt")

# contents of result.txt
jan. 01 2018
It's a nice day.

jan. 02 2018
Today is a rainy day.

Notice that it doesn't say Jan, but jan. This is due to my local which doesn't match to what you may be used to.

> Sys.getlocale()
[1] "LC_COLLATE=Slovenian_Slovenia.1250;LC_CTYPE=Slovenian_Slovenia.1250;LC_MONETARY=Slovenian_Slovenia.1250;LC_NUMERIC=C;LC_TIME=Slovenian_Slovenia.1250"

If I set time locale to something else (may only work on windows)

> Sys.setlocale(category = "LC_TIME", locale = "English_United States.1252")

the result is

> xy
[1] "Jan 01 2018"           "It's a nice day."      ""                      "Jan 02 2018"          
[5] "Today is a rainy day."

Upvotes: 1

Esben Eickhardt
Esben Eickhardt

Reputation: 3852

Try this out:

# Loading data
data <- readLines("diary.txt")

# Identifying lines with dates
date_lines <- grep("^[[:digit:]]", data)

# Creating dates
data[date_lines] <- format(as.POSIXct(data[date_lines], format = "%Y.%m.%d"), "%b %d %Y")

# Writing to new file
fileConn<-file("diary_fixed.txt")
writeLines(data, fileConn)
close(fileConn)

Upvotes: 1

Related Questions