【问题标题】:R - system commands with multiple pipe do not return stdoutR - 具有多个管道的系统命令不返回标准输出
【发布时间】:2015-11-01 11:14:10
【问题描述】:

重击:

ps -aux | grep -E "^.*\b[^grep](python).*(runserver).*$" 2>/dev/null | tr -s " " | cut -d " " -f 2

它返回正确的结果。 (例如)

1450
1452

R 中的相同代码

vLog <- system('ps -aux | grep -E "^.*\b[^grep](python).*(runserver).*$" 2>/dev/null | tr -s " " | cut -d " " -f 2', intern = TRUE)

返回character(0)

【问题讨论】:

    标签: r bash pipe system


    【解决方案1】:

    只需将\b 替换为\\b 并注意[^grep] 匹配任何字符但不匹配grep

    vLog <- system('ps -aux | grep -E "^.*\\b[^grep](python).*(runserver).*$" 2>/dev/null | tr -s " " | cut -d " " -f 2', intern = TRUE)
    

    示例:

    > system('ps -aux | grep -E "^.*\\bpython" 2>/dev/null | tr -s " " | cut -d " " -f 2', intern = TRUE)
    [1] "2519" "2526" "3285" "3291"
    > system('ps -aux | grep -E "^.*\bpython" 2>/dev/null | tr -s " " | cut -d " " -f 2', intern = TRUE)
    character(0)
    

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 2019-09-03
      • 2021-04-29
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多