[R] iterators : checkFunc with ireadLines

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed May 27 14:41:43 CEST 2020


On Wed, 27 May 2020 10:56:42 +0200
Laurent Rhelp <LaurentRHelp using free.fr> wrote:

> May be it is because I work with MS windows ?

That is probably the case.

On Windows, pipe() invokes "%COMSPEC% /c <description>", and the
rules of command line quoting are different between POSIX shell and
cmd.exe + runtimes of Windows applications [*].

Can you run raku / perl / grep / awk from the cmd prompt? If not, check
your %PATH% variable. Either way, shQuote() is supposed to be able to
handle the quoting madness for us, the inner call performing the
quoting for the runtime and the outer call escaping for the cmd.exe
itself:

pipe(shQuote(
 paste(
  'raku', '-e',
  shQuote('.put for lines.grep( / ^^N053 | ^^N163 /, :p );'),
  'Laurents.txt'
 ),
 type = 'cmd2'
))

pipe(shQuote(
 paste('grep', '-E', shQuote('^(N053|N163)'), 'test.txt'),
 'cmd2'
))

pipe(shQuote(
 paste('awk', shQuote('($1 ~ "^(N053|N163)")'), 'test.txt'),
 'cmd2'
))

pipe(shQuote(
 paste(
  'perl', '-CSD', '-F', shQuote('\\s+'), '-lE',
  shQuote('print join qq{\\t}, @F if $F[0] =~ /^(N053|N163)$/'),
  'test.txt'
 ), 'cmd2'
))

This way, we can even pretend that we are passing an _array_ of command
line arguments to the child process, like K&R intended, and not
building a command _line_ to be interpreted by the command line
interpreter and application runtime.

-- 
Best regards,
Ivan

[*] In POSIX, the command line is an array of NUL-terminated C strings.
In Windows, the command line is a single NUL-terminated C string, so
the runtime of the application is responsible for obtaining an array of
command line arguments from that:
https://docs.microsoft.com/ru-ru/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way



More information about the R-help mailing list