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


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



I need to run awk from within R to add spaces at the end of each line of a file, to ensure that it has consistently a fixed width throughout. Like this:


awk


$ cat -e file
1$
12$
123$
1234$
$ awk '{printf "%-10sn", $0}' file | cat -e
1 $
12 $
123 $
1234 $
12345 $



However I have trouble doing this from within R. How to correctly escape system calls from inside R deals with something similar, but it doesn't seem to work for me.


> ' awk '{printf "%-180sn", $0}' file '
[1] " awk '{printf "%-180sn", $0}' file "
> system(' awk '{printf "%-180sn", $0}' file ')
awk: cmd. line:1: {printf "%-180s
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: {printf "%-180s
awk: cmd. line:1: ^ syntax error



I also tried system(" awk '{printf "%-180sn", $0}' file "), with the exact same error. I'm not sure why this fails, but it seems to be related to the mixed quotes. Any ideas how to make this work?


system(" awk '{printf "%-180sn", $0}' file ")





This completely blows me away. Are you saying that R is unable to do this without calling a system command?
– kvantour
2 days ago




1 Answer
1



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



n


printf()


awk


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



or with a variable


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





Well, that was silly of me, and now the error message makes more sense... Thanks!
– landroni
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV