【问题标题】:system command in R and escaping issueR中的系统命令和转义问题
【发布时间】:2017-07-29 16:40:44
【问题描述】:

我正在尝试清理一些嵌入的空字符是一个 csv 文件,使用下面的这个命令

tr -cd '\11\12\15\40-\176' datafile.csv

当我直接在 linux 终端上运行这个命令时,这个命令可以正常工作。但是,当我尝试使用下面这样的系统命令通过 R 执行此操作时

system(" 'tr -cd '\11\12\15\40-\176' datafile.csv' ")

我得到一个错误,

sh: tr -cd : command not found
: command not found

我怀疑这与逃避一些刺痛有关,需要帮助解决此错误。

【问题讨论】:

    标签: r linux null system


    【解决方案1】:

    我相信是额外的一对单引号把事情搞砸了。 这对我有用,看看你能不能让它工作。

    system("printf '\11 eleven \12 \13 fifty-eight \15 \
            55 \40 \150 \176 \n one, two three\n' > so-test.txt")
    system("cat so-test.txt")
    system("tr -cd '\11\12\15\40-\176' < so-test.txt > so-test2.txt")
    system("diff test.txt test2.txt")
    

    PS:总是在提出此类问题时包含一个可重复的示例。不知道我的例子有没有道理,我觉得你删除空字符的方法看起来有点奇怪。

    【讨论】:

    • 第一个选项 system("printf '\11 eleven \12 \13 fifty-eight \15 \ 55 \40 \150 \176 \n one, two three\n' &gt; so-test.txt") 导致一个空文件。第三个选项system("tr -cd '\11\12\15\40-\176' &lt; so-test.txt &gt; so-test2.txt") 有效。我不得不将我的文件从 CSV 更改为 TXT。感谢您的建议。
    猜你喜欢
    • 2011-01-03
    • 2020-09-17
    • 2014-09-07
    • 2015-01-01
    • 1970-01-01
    • 2012-07-28
    • 2019-01-05
    • 2019-03-05
    • 1970-01-01
    相关资源
    最近更新 更多