On 6/14/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > Here is yet another solution. This is the simplest so far. > Lines.raw is as before and the output is a 3 column character > matrix.
section <- "" > f <- function(x) { > if (length(x) == 1) section <<- gsub("[\\[\\]]", "", x) > if (length(x) <= 1) return() > return(c(x, section)) > } > # Lines <- readLines("myfile.ini") > Lines <- readLines(textConnection(Lines.raw)) > do.call("rbind", lapply(strsplit(Lines, "="), f)) The corresponding awk code fits in one line '/\[/{a=$1;next}{print $1,$2,a}'. With the example.ini: $ awk -F"=" '/\[/{a=$1;next}{print $1,$2,a}' example.ini var1 value1 [Section1] var2 value2 [Section1] A value3 [Section2] B value4 [Section2] The output can then be imported in R with read.table. I know, I am not playing by the rules here... :) But with programming languages, like with human languages, it pays to be bi or tri-lingual (or polyglot, if one can). I also realise that under Windows, it means using the command line, something that not so many people are comfortable with nowadays. One reason people insist on using only one language to do everything may be due to the awkwardness and limitations of the default scripting language under Windows (DOS). Having everything done inside one single R script can seem simpler. But a divide-and-conquer approach, with a few small scripts, can actually work better in some complex cases. I tend to do as much as possible in R but for all serious data analysis tasks, I use Makefile to "glue" the various stages of processing. Data preprocessing stages (performed with R or other tools) create files that are then processed with R. One advantage is that preprocessing is performed only when raw input data change; but the most important, is that when I come back years later, I can follow the exact flow of transformations. Again, "make" (like awk or R) is not without limits and idiosyncrasies. If someone has a simpler solution, I am interested to hear about it. Christophe -- Christophe Pallier (http://www.pallier.org) [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.