在R中粘贴一个grep字符串

时间:2020-12-04 16:58:46

need to write a file with a varierty of paramters. The command looks like

需要编写一个包含各种参数的文件。该命令看起来像

awk 'match($0, /^\[[0-9]*\]/) {print substr($0, RSTART + 1, RLENGTH - 2)}' testdata > out

when I then try to paste it in R

当我然后尝试将其粘贴在R中

 paste("awk 'match($0, /^\[[0-9]*\]/) {print substr($0, RSTART + 1, RLENGTH - 2)}'")
    Error: '\[' is an unrecognized escape in character string starting ""awk 'match($0, /^\[

can somebody tell me how I get around that error and paste that string correctly in R?

谁能告诉我如何绕过该错误并在R中正确粘贴该字符串?

1 个解决方案

#1


You need to escape the \ character, as \[is not a valid escape sequence to \\[. A similar argument holds for \].

你需要转义\字符,因为\ [不是一个有效的转义序列到\\ [。类似的论点适用于\]。

The correct call is thus

因此,正确的呼叫

paste("awk 'match($0, /^\\[[0-9]*\\]/) {print substr($0, RSTART + 1, RLENGTH - 2)}'")

#1


You need to escape the \ character, as \[is not a valid escape sequence to \\[. A similar argument holds for \].

你需要转义\字符,因为\ [不是一个有效的转义序列到\\ [。类似的论点适用于\]。

The correct call is thus

因此,正确的呼叫

paste("awk 'match($0, /^\\[[0-9]*\\]/) {print substr($0, RSTART + 1, RLENGTH - 2)}'")