Skip to content

Getting error while running unix commands within R using system() function

3 messages · siddu479, William Dunlap

#
Hello All,

   I use Cygwin ( unix on windows) heavily for all my text data processing.
Also use Cygwin inbuilt *R* to do numerical processing.
*My aim is to integrate R and unix commands to avoid heavy memory usage that
R takes normally.*

I can run many unix commands using system("some unix command or sh script.sh
or even R file itself ") inside R.

But, When I tried to run the command *awk '{print "Hello"$1}' infile.txt* to
prefix "Hello" to my first column of file infile.txt, like below in *R *
*system(" awk '{print "Hello"$1}' infile.txt") *, I am getting the below
error, 
*Error: unexpected symbol in "system("awk '{print "Hello"*
I tried even like *system('awk '{print "Hello"$1}' infile.txt')*, but still
the same error.

This particular error is hampering my entire work, other wise I found this
is excellent way processing the text files at the same time reading back and
forth file outputs from R to unix and vice versa.

Can some one give me provide some solution to this problem ??








-----
Sidda
Business Analyst Lead
Applied Materials Inc.

--
View this message in context: http://r.789695.n4.nabble.com/Getting-error-while-running-unix-commands-within-R-using-system-function-tp4647656.html
Sent from the R help mailing list archive at Nabble.com.
#
To diagnose a problem in R it pays to pull apart your expression
into a sequence of simpler ones.  E.g., replace your expression
  system(" awk '{print "Hello"$1}' infile.txt")
with the sequence
  sysCmd <- " awk '{print "Hello"$1}' infile.txt"
  system(sysCmd)
and you will see an error when running the first one, showing that
this has nothing to do with the system() function:
  > sysCmd <- " awk '{print "Hello"$1}' infile.txt"
  Error: unexpected symbol in "sysCmd <- " awk '{print "Hello"
Note where the error message ends, at a double quote, suggesting
that quoting may be the problem.

To put a double quote inside a double-quoted string you must
put a backslash in front of it
  > sysCmd <- " awk '{print \"Hello\"$1}' infile.txt"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
Thanks William,

    This solved my problem very well. Look like I asked a very trivial
question.






-----
Sidda
Business Analyst Lead
Applied Materials Inc.

--
View this message in context: http://r.789695.n4.nabble.com/Getting-error-while-running-unix-commands-within-R-using-system-function-tp4647656p4647676.html
Sent from the R help mailing list archive at Nabble.com.