Bash | Importing a .txt to .txt file, line by line with 'While Read'











up vote
0
down vote

favorite












I know this question appears rather frequently, but I cannot seem to find a solution on importing a .txt file line by line to another .txt with the While Read command:



line="input.txt"

while read -r line; do
printf '%sn' "$line"
done < outputfile.txt


I tried different alternatives, tried them all as variables, using the variables with their directories, tried cat on the input file, use of echo and printf but to no avail. Thanks in advance for any and all advance!










share|improve this question






















  • Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
    – Gordon Davisson
    Nov 21 at 17:32










  • I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
    – Daniel Jameson
    Nov 21 at 17:40






  • 2




    you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
    – rAlen
    Nov 21 at 17:43








  • 1




    while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
    – William Pursell
    Nov 21 at 17:55












  • Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
    – Daniel Jameson
    Nov 22 at 18:13















up vote
0
down vote

favorite












I know this question appears rather frequently, but I cannot seem to find a solution on importing a .txt file line by line to another .txt with the While Read command:



line="input.txt"

while read -r line; do
printf '%sn' "$line"
done < outputfile.txt


I tried different alternatives, tried them all as variables, using the variables with their directories, tried cat on the input file, use of echo and printf but to no avail. Thanks in advance for any and all advance!










share|improve this question






















  • Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
    – Gordon Davisson
    Nov 21 at 17:32










  • I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
    – Daniel Jameson
    Nov 21 at 17:40






  • 2




    you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
    – rAlen
    Nov 21 at 17:43








  • 1




    while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
    – William Pursell
    Nov 21 at 17:55












  • Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
    – Daniel Jameson
    Nov 22 at 18:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I know this question appears rather frequently, but I cannot seem to find a solution on importing a .txt file line by line to another .txt with the While Read command:



line="input.txt"

while read -r line; do
printf '%sn' "$line"
done < outputfile.txt


I tried different alternatives, tried them all as variables, using the variables with their directories, tried cat on the input file, use of echo and printf but to no avail. Thanks in advance for any and all advance!










share|improve this question













I know this question appears rather frequently, but I cannot seem to find a solution on importing a .txt file line by line to another .txt with the While Read command:



line="input.txt"

while read -r line; do
printf '%sn' "$line"
done < outputfile.txt


I tried different alternatives, tried them all as variables, using the variables with their directories, tried cat on the input file, use of echo and printf but to no avail. Thanks in advance for any and all advance!







bash unix while-loop line-by-line






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 17:16









Daniel Jameson

11




11












  • Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
    – Gordon Davisson
    Nov 21 at 17:32










  • I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
    – Daniel Jameson
    Nov 21 at 17:40






  • 2




    you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
    – rAlen
    Nov 21 at 17:43








  • 1




    while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
    – William Pursell
    Nov 21 at 17:55












  • Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
    – Daniel Jameson
    Nov 22 at 18:13


















  • Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
    – Gordon Davisson
    Nov 21 at 17:32










  • I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
    – Daniel Jameson
    Nov 21 at 17:40






  • 2




    you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
    – rAlen
    Nov 21 at 17:43








  • 1




    while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
    – William Pursell
    Nov 21 at 17:55












  • Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
    – Daniel Jameson
    Nov 22 at 18:13
















Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
– Gordon Davisson
Nov 21 at 17:32




Can you explain what’s going wrong - that is, what is happening, and how is that different from what you want to happen? Also, exactly what do you mean by “import”?
– Gordon Davisson
Nov 21 at 17:32












I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
– Daniel Jameson
Nov 21 at 17:40




I want it to read the input txt file into the output txt file line by line, though this code runs without errors, nothing gets inputted into the outputfile and it remains blank
– Daniel Jameson
Nov 21 at 17:40




2




2




you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
– rAlen
Nov 21 at 17:43






you are using outputfile.txt as input in your example, < outputfile.txt means it takes data from that file, not put it in that file, it assigns $line to line it gets from output.txt file, see answer by Mike Q below, you don't need line="input.txt" at all, just put < input.txt instead of outputfile.txt and output the contents of loop to outputfile.txt
– rAlen
Nov 21 at 17:43






1




1




while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
– William Pursell
Nov 21 at 17:55






while read -r line; do printf '%sn' "$line"; done < inputfile.txt > outputfile.txt
– William Pursell
Nov 21 at 17:55














Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
– Daniel Jameson
Nov 22 at 18:13




Thank you William as that code has sorted it and thanks to everyone else, you would get votes but I don't think I got that option yet
– Daniel Jameson
Nov 22 at 18:13












1 Answer
1






active

oldest

votes

















up vote
2
down vote













I think you are just trying to combine files ?



cat input.txt >> outputfile.txt 


Not suggesting this but as an FYI :



file="input.txt"
while IFS= read -r line ;do
echo "${line}" >> outputfile.txt
done < "${file}"





share|improve this answer



















  • 3




    Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
    – Charles Duffy
    Nov 21 at 18:13








  • 2




    Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
    – glenn jackman
    Nov 21 at 19:50










  • @CharlesDuffy updated, thx~!
    – Mike Q
    Nov 21 at 20:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417419%2fbash-importing-a-txt-to-txt-file-line-by-line-with-while-read%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













I think you are just trying to combine files ?



cat input.txt >> outputfile.txt 


Not suggesting this but as an FYI :



file="input.txt"
while IFS= read -r line ;do
echo "${line}" >> outputfile.txt
done < "${file}"





share|improve this answer



















  • 3




    Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
    – Charles Duffy
    Nov 21 at 18:13








  • 2




    Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
    – glenn jackman
    Nov 21 at 19:50










  • @CharlesDuffy updated, thx~!
    – Mike Q
    Nov 21 at 20:49















up vote
2
down vote













I think you are just trying to combine files ?



cat input.txt >> outputfile.txt 


Not suggesting this but as an FYI :



file="input.txt"
while IFS= read -r line ;do
echo "${line}" >> outputfile.txt
done < "${file}"





share|improve this answer



















  • 3




    Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
    – Charles Duffy
    Nov 21 at 18:13








  • 2




    Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
    – glenn jackman
    Nov 21 at 19:50










  • @CharlesDuffy updated, thx~!
    – Mike Q
    Nov 21 at 20:49













up vote
2
down vote










up vote
2
down vote









I think you are just trying to combine files ?



cat input.txt >> outputfile.txt 


Not suggesting this but as an FYI :



file="input.txt"
while IFS= read -r line ;do
echo "${line}" >> outputfile.txt
done < "${file}"





share|improve this answer














I think you are just trying to combine files ?



cat input.txt >> outputfile.txt 


Not suggesting this but as an FYI :



file="input.txt"
while IFS= read -r line ;do
echo "${line}" >> outputfile.txt
done < "${file}"






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 20:49

























answered Nov 21 at 17:26









Mike Q

2,40612237




2,40612237








  • 3




    Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
    – Charles Duffy
    Nov 21 at 18:13








  • 2




    Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
    – glenn jackman
    Nov 21 at 19:50










  • @CharlesDuffy updated, thx~!
    – Mike Q
    Nov 21 at 20:49














  • 3




    Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
    – Charles Duffy
    Nov 21 at 18:13








  • 2




    Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
    – glenn jackman
    Nov 21 at 19:50










  • @CharlesDuffy updated, thx~!
    – Mike Q
    Nov 21 at 20:49








3




3




Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
– Charles Duffy
Nov 21 at 18:13






Somehow your code has "smart quotes" in it -- where it should be ". Consequently, it won't work if copied-and-pasted. (BTW, speaking of quotes, consider <"$file", with double quotes around the expansion, to avoid bugs with shell versions that don't suppress string-splitting on redirection -- unfortunately, this includes versions of bash still in use today, which can cause surprising "ambiguous redirect" errors).
– Charles Duffy
Nov 21 at 18:13






2




2




Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50




Or, while IFS= read -r line; do echo "$line"; done < input >> output with the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50












@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49




@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417419%2fbash-importing-a-txt-to-txt-file-line-by-line-with-while-read%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista