I have a text file containing data like this:
This is just text
-------------------------------
Username: SOMETHI C: [Text]
Account: DFAG Finish time: 1-JAN-2011 00:31:58.91
Process ID: 2028aaB Start time: 31-DEC-2010 20:27:15.30
This is just text
-------------------------------
Username: SOMEGG C: [Text]
Account: DFAG Finish time: 1-JAN-2011 00:31:58.91
Process ID: 20dd33DB Start time: 12-DEC-2010 20:27:15.30
This is just text
-------------------------------
Username: SOMEYY C: [Text]
Account: DFAG Finish time: 1-JAN-2011 00:31:58.91
Process ID: 202223DB Start time: 15-DEC-2010 20:27:15.30
Is there a way to extract Username, Finish time, Start time from this kind of data? I'm looking for some starting point usign R or Powershell.
One of the easiest tasks is retrieving all text from an existing text file. For most text files, a PowerShell scripter can use the Get-Content cmdlet. The Get-Content cmdlet is a very popular PowerShell cmdlet that will retrieve all text from a text file specified by the Path parameter.
When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.
R may not be the best tool to process text files, but you can proceed as follows: identify the two columns by reading the file as a fixed-width file, separate the fields from their value by splitting the strings on the colons, add an "id" column, and put everything back in order.
# Read the file
d <- read.fwf("A.txt", c(37,100), stringsAsFactors=FALSE)
# Separate fields and values
d <- d[grep(":", d$V1),]
d <- cbind(
do.call( rbind, strsplit(d$V1, ":\\s+") ),
do.call( rbind, strsplit(d$V2, ":\\s+") )
)
# Add an id column
d <- cbind( d, cumsum( d[,1] == "Username" ) )
# Stack the left and right parts
d <- rbind( d[,c(5,1,2)], d[,c(5,3,4)] )
colnames(d) <- c("id", "field", "value")
d <- as.data.frame(d)
d$value <- gsub("\\s+$", "", d$value)
# Convert to a wide data.frame
library(reshape2)
d <- dcast( d, id ~ field )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With