Awk Script in R

Awk script in R

you should be able to do

system('your awk command here')

If you have single quotes in your awk command, either do as Hong Ooi suggests and put your awk command in a separate file and then just run that from the system command or escape the single quotes:

> system('echo \'hello guy!\' ')
hello guy!

How to execute an awk call with mixed quotes in R?

The idea is right, you just need to escape the \ in \n. Since it is handled by the printf() inside awk you need to escape it to let it be treated literally

system("awk '{printf \"%-10s\\n\",$0}' file")

or with a variable

awkcommand <- "awk '{printf \"%-10s\\n\",$0}' file"
system(awkcommand)

Using Awk Commands in R to Extract Data

If g is the result of the grep then:

read.table(text = g)[, 4]

Is it possible to run awk Script from R Shiny App?

You can use shell, shell.exec or system (allows you to run a command from R).

Piping awk in R

This seems to work:

read.csv(pipe("awk -v FIELDWIDTHS='2 2 6' -v OFS=',' '($2==\"A \"){ $1=$1 \'\'; print }'<rawk.txt"),header=FALSE)

V1 V2 V3
1 10 A 10001
2 10 A 10002
3 10 A 10003

But I'm not sure what the $1=$1 is all about...

Combining R + awk + bash commands

Besides Vincent's hint of using system("awk ...", intern=TRUE), you can also use the pipe() function that is part of the usual 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 am piping a command into awk and then read all the output from awk, that could 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>

R system call to awk fail

This often happens when trying to use other languages with R, e.g. Python. If you haven't added the paths to your Windows system path then you haven't told RStudio where to find the executables.

The root of Cygwin is normally found at C:\cygwin64 (but could vary by your installation) so find the install and look for the bin folder. In there should be the awk executable, but it is normally just a symlink to a gawk executable (verify yourself) so add that to the PATH, e.g.:

Sys.setenv(PATH = paste("C:/cygwin64/bin/gawk", Sys.getenv("PATH"), sep = ":"))

NOTE: This does not add permanently so you must start at the beginning of each session or add to your Windows path to have it recognized permanently.

System (awk) command saved as a variables error

There are a couple errors in the code. The code below works for me and (per your comment) returns a two column data frame:

a <- system("awk '{ if (/foo/) print 2 \"\t\" $0; else print 1 \"\t\" $0; }' file", intern = TRUE)
DF = read.table(text = a, sep = "\t")
DF
#> V1 V2
#> 1 2 foo
#> 2 1 bar
#> 3 1 asd

summary(DF)
#> V1 V2
#> Min. :1.000 Length:3
#> 1st Qu.:1.000 Class :character
#> Median :1.000 Mode :character
#> Mean :1.333
#> 3rd Qu.:1.500
#> Max. :2.000

Created on 2022-03-23 by the reprex package (v2.0.1)



Related Topics



Leave a reply



Submit