How to echo content into a series of new file generated by cat
up vote
1
down vote
favorite
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
cat echo
New contributor
add a comment |
up vote
1
down vote
favorite
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
cat echo
New contributor
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 4 at 15:52
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
cat echo
New contributor
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
cat echo
cat echo
New contributor
New contributor
edited Dec 4 at 15:48
New contributor
asked Dec 4 at 15:32
Molly_K
83
83
New contributor
New contributor
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 4 at 15:52
add a comment |
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 4 at 15:52
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 4 at 15:52
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 4 at 15:52
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
Indeed, thatEOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f.man bash
, "Here Documents"
– RudiC
Dec 4 at 18:26
add a comment |
up vote
0
down vote
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
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
Indeed, thatEOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f.man bash
, "Here Documents"
– RudiC
Dec 4 at 18:26
add a comment |
up vote
1
down vote
accepted
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
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
Indeed, thatEOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f.man bash
, "Here Documents"
– RudiC
Dec 4 at 18:26
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
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
answered Dec 4 at 15:49
RudiC
3,7171312
3,7171312
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
Indeed, thatEOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f.man bash
, "Here Documents"
– RudiC
Dec 4 at 18:26
add a comment |
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
Indeed, thatEOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f.man bash
, "Here Documents"
– RudiC
Dec 4 at 18:26
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
Thank you RudiC, I did not know the function "EOF"; I found this post that explained the usage. unix.stackexchange.com/questions/300129/how-do-i-use-cat-eof
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 at 15:58
The EOF command works great. Thanks again!
– Molly_K
Dec 4 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 4 at 18:26
Indeed, that
EOF
is not a command nor a function, but a deliberately chosen "word" / "delimiter", c.f. man bash
, "Here Documents"– RudiC
Dec 4 at 18:26
add a comment |
up vote
0
down vote
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
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
add a comment |
up vote
0
down vote
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
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
edited Dec 4 at 15:53
answered Dec 4 at 15:48
Peschke
2,400924
2,400924
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
add a comment |
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
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 4 at 15:50
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 4 at 15:50
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 at 15:57
@Molly_K: I answered before your first edit. See my edited answer.
– Peschke
Dec 4 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 4 at 16:08
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 4 at 16:08
add a comment |
Molly_K is a new contributor. Be nice, and check out our Code of Conduct.
Molly_K is a new contributor. Be nice, and check out our Code of Conduct.
Molly_K is a new contributor. Be nice, and check out our Code of Conduct.
Molly_K is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485923%2fhow-to-echo-content-into-a-series-of-new-file-generated-by-cat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 4 at 15:52