G.A.7
G.A.7

Reputation: 13

Syntax error near unexpected token `(' when using R system() function

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.

Upvotes: 0

Views: 1567

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295345

<() 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

Upvotes: 2

Related Questions