R - 具有多个管道的系统命令不返回stdout

时间:2015-08-09 12:40:12

标签: r bash pipe system

击:

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)

1 个答案:

答案 0 :(得分:3)

只需将\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)