awk: A Simple Front-end to Awk

Description Usage Arguments Examples

View source: R/awk.R

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)

Example output

sh: 1: awk: Permission denied
Warning message:
running command 'awk ' { print $1 } ' /work/tmp/tmp/RtmpkzcRWn/file2cda6f55f169' had status 126 
      [,1]
 [1,] "1" 
 [2,] "2" 
 [3,] "3" 
 [4,] "4" 
 [5,] "5" 
 [6,] "6" 
 [7,] "7" 
 [8,] "8" 
 [9,] "9" 
[10,] "10"

Kmisc documentation built on May 29, 2017, 1:43 p.m.

Related to awk in Kmisc...