A Simple Front-end to Awk

Share:

Description

This function provides a simple front-end to awk. It assumes that you have awk available and in your PATH.

Usage

1
2
awk(code, file, BEGIN = NULL, END = NULL, vars = NULL, fs = NULL,
  out = TRUE, verbose = FALSE)

Arguments

code

The awk code you want to put in the main execution block.

file

The file we are running awk on.

BEGIN

A block of code to include as though it were within the BEGIN block.

END

A block of code to include as though it were within the END block.

vars

A named list, whereby variables are assigned so that name=value.

fs

The field separator (passed to -F).

out

The location to output the result of the computation. If this is TRUE, we intern the process and bring the results back into the R session. Otherwise, it should be a string specifying the output path for a file.

verbose

Output the generated awk code?

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## Not run: 
dat <- data.frame(
  x=1:10,
  y=letters[1:10],
  z=LETTERS[1:10]
)

tempfile <- tempfile()

write.table(dat,
  file=tempfile,
  row.names=FALSE,
  col.names=FALSE,
  quote=FALSE
)

x <- awk("print $1", tempfile)
## note that it is read in as type 'character'
print( cbind( x, dat$x ) )

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.