1

I would like to write a loop to generate 22 R scripts with contents generated in echo. What went wrong in this loop that I could not get the 22 files with the corresponding content?

I tested echo "i want that file" > file.r

It works fine. Then I tested, it treated as two seperate process. First it echo the text, then it generated 22 empty files.

for i in `seq 1 22`; do
    echo "a <- read.delim('file${i}.txt', header=T, check.names=FALSE);
        library('splitstackshape');
        loc1 <- cSplit(a, 'V1', ':');
        rm(a);
        out <- cbind.data.frame(loc1$V1_1, loc1$V1_2);
        write.table(out, 'out_file${i}.txt', sep='\t', row.names=F, quote=F)"; 
> build_file${i}.r; done

Then I tried this.. still not working..

    for i in `seq 1 22`; do
    echo "a <- read.delim("file${i}.txt", header=T, check.names=FALSE);
    library("splitstackshape");
    loc1 <- cSplit(a, "V1", ":");
    rm(a);
    out <- cbind.data.frame(loc1$V1_1, loc1$V1_2);
    write.table(out, "out_file${i}.txt", sep="\t", row.names=F, quote=F)";
    cat > loc_chr${i}.r; done
Molly_K
  • 161
  • Could someone also let me know why I got down vote for this question? Is it because I should not be asking how to use cat echo in a loop or did I violate any kind of rules on this site? Many thanks. – Molly_K Dec 04 '18 at 15:52

2 Answers2

1

Not sure I completely follow the quoting in your script, and what you want the cat command to do, as it reads from the non-redirected stdin, i.e. presumably your keyboard. How about something like

for i in `seq 1 22`
  do cat <<- EOF > loc_chr${i}.r
"a <- read.delim("file${i}.txt", header=T, check.names=FALSE);
    library("splitstackshape");
    loc1 <- cSplit(a, "V1", ":");
    rm(a);
    out <- cbind.data.frame(loc1$V1_1, loc1$V1_2);
    write.table(out, "out_file${i}.txt", sep="\t", row.names=F, quote=F)"
EOF
   done
RudiC
  • 8,969
  • Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. https://unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof – Molly_K Dec 04 '18 at 15:58
  • Indeed, that EOF is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f. man bash, "Here Documents" – RudiC Dec 04 '18 at 18:26
0

Your first code sample isn't working because it has > build_file${i}.r on it's own line, which is redirecting nothing into 20 files. Append > build_file${i}.r to the end of your echo.

Your second code sample isn't working because of cat at the bottom. You don't supply what you want cat to concatenate, therefore it's waiting for input.

You should delete the cat line, and append > loc_chr${i}.r to the end of your echo.

for i in `seq 1 22`; do
echo "a <- read.delim("file${i}.txt", header=T, check.names=FALSE);
library("splitstackshape");
loc1 <- cSplit(a, "V1", ":");
rm(a);
out <- cbind.data.frame(loc1$V1_1, loc1$V1_2);
write.table(out, "out_file${i}.txt", sep="\t", row.names=F, quote=F)" > loc_chr${i}.r
done
Peschke
  • 4,148
  • Hi @Peschke, thanks for the suggestions. I tested a short script and came to the same conclusion, but as I just updated in the post, it did not generate the ideal results. Instead, there were 22 separate empty files while I see echo working at the terminal printing out all the text. – Molly_K Dec 04 '18 at 15:50
  • @Molly_K: I answered before your first edit. See my edited answer. – Peschke Dec 04 '18 at 15:57
  • Thank you @Peschke, I learned that I had an extra ";" before I redirect the echo content to the output file. I will look into the reason why is that. Thank you for showing me how to work around the cat and echo function. – Molly_K Dec 04 '18 at 16:08