Combining R + awk + bash Commands

I want to combine awk and R-language. The fact is that I have a set of * .txt files in the specified directory and that I do not know the length of the header from the files. In some cases, I have to skip 25 lines, and in others, skip 27, etc. So I want to type some awk commands to skip the number of lines. Once I have this value, I can start processing the data with R.

Also, in the R file, I am combining R a bash, so my code looks like this:

!/usr/bin/env Rscript ... argv <- commandArgs(T) **error checking...** import_file <- argv[1] export_file <- argv[2] **# your function call** format_windpro(import_file, export_file) 

Where and how can I enter the awk command. Thanks!

I tried to do what you told me about awk commands, and I still get an error message. The program does not recognize my command, so I can not enter the number of lines to go to my function. Here is my code:

** nline <- paste ('$ (grep -n' m / s 'import_file | awk -F ":"' {print $ 1} ')')

nline <- scan (pipe (nline), quiet = T) **

I am looking for the m / s template in the first column to find out where I have the header text. I am using R under w7.

+7
source share
2 answers

Besides the Vincent hint of using system("awk ...", intern=TRUE) , you can also use the pipe() function, which is part of regular text connections:

 R> sizes <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'")) R> summary(sizes) V1 Min. : 0 1st Qu.: 482 Median : 4096 Mean : 98746 3rd Qu.: 13952 Max. :27662342 R> 

Here I write a command in awk and then read all the output from awk , which can also be a single line:

 R> cmd <- "ls -l /tmp | awk '!/^total/ {sum = sum + $5} END {print sum}'" R> totalsize <- scan(pipe(cmd), quiet=TRUE) R> totalsize [1] 116027050 R> 
+8
source

You can use system to start an external program from R.

 system("gawk --version", intern=TRUE) 
+6
source

All Articles