Learn R Programming

Kmisc (version 0.5.0)

awk: A Simple Front-end to Awk

Description

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

Usage

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

Run this code
## 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)

Run the code above in your browser using DataLab