使用 R system() 函数时,意外标记 `(' 附近出现语法错误

发布于 2025-01-17 07:23:00 字数 642 浏览 6 评论 0原文

这里我有多个文件,比如 3 个具有相同行和列的文件,现在我想从每个文件中提取第二列并将它们粘贴到一个新文件中。当我在 linux bash 中运行以下命令时,它起作用了。

paste <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text1.txt) <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text2.txt) <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text3.txt) > output.txt

当我尝试使用 R 脚本通过 system() 函数运行此命令时,

cmd = paste0("paste ", paste0("<(awk 'BEGIN{OFS=\"\t\"}(NR>1){print $2}' text", 1:3, ".txt) ", collapse = ""), " > output.txt", collapse = "")
system(cmd)

它返回错误:

syntax error near unexpected token `('

任何人都可以帮助我。感谢任何帮助。

Here I have multiple files say 3 files with same rows and columns, now I want to extract the second column from each file and paste them in a new file. When I run the following command in linux bash,its working.

paste <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text1.txt) <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text2.txt) <(awk 'BEGIN{OFS="\t"}(NR>1){print $2}' text3.txt) > output.txt

While when I try to use R script to run this command via system() function by

cmd = paste0("paste ", paste0("<(awk 'BEGIN{OFS=\"\t\"}(NR>1){print $2}' text", 1:3, ".txt) ", collapse = ""), " > output.txt", collapse = "")
system(cmd)

it returns error:

syntax error near unexpected token `('

Can anyone help me out. Appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2025-01-24 07:23:00

<() 仅在扩展 shell 中可用(ksh93、bash、zsh 等); system() 使用 sh。但是,您可以编写与 sh 配合使用的等效脚本:

while read _ a _ <&3 && read _ b _ <&4 && read _ c _ <&5; do
  printf '%s\t%s\t%s\n' "$a" "$b" "$c"
done 3<text1.txt 4<text2.txt 5<text3.txt >output.txt

<() is only available in extended shells (ksh93, bash, zsh, etc); and system() uses sh. However, you can write an equivalent script that works with sh:

while read _ a _ <&3 && read _ b _ <&4 && read _ c _ <&5; do
  printf '%s\t%s\t%s\n' "$a" "$b" "$c"
done 3<text1.txt 4<text2.txt 5<text3.txt >output.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文