readLines
can read the data in, then grep
the comment character. In the function below, the comment character defaults to the question's "#"
.
fun <- function(file, char = "#"){
x <- readLines(con = file)
y <- x[which(diff(grep(char, x)) != 1)]
unlist(strsplit(y, " "))[-1]
}
fun("filename.txt")
#[1] "values" "intensities"
If you have a long datafile and it doesn't fit in memory and have awk
available, the following solution can read the data without memory problems.
read_awk <- function(file, char = "#"){
cmd <- "awk"
pattern <- paste0("/^[^", char, "]/")
awkcmd <- paste0("'", pattern, " {print NR - 1; exit 0}'")
args <- c(awkcmd, file)
out <- system2(command = cmd, args = args, stdout = TRUE)
as.integer(out)
}
fun_awk <- function(file, char = "#"){
n <- read_awk(file, char = char)
x <- scan(file = file, what = character(), sep = "\n", skip = n - 1, nlines = 1)
unlist(strsplit(x, " "))[-1]
}
fun_awk("filename.txt")
#Read 1 item
#[1] "values" "intensities"
Data
"filename.txt"
is the following file:
# Comment line 1
# Comment line 2
# ... many more lines
# values intensities
5.556667e+00 4.008450e+02
5.581000e+00 4.008770e+02
# End comments