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!
bash unix while-loop line-by-line
add a comment |
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!
bash unix while-loop line-by-line
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
add a comment |
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!
bash unix while-loop line-by-line
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
bash unix while-loop line-by-line
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
add a comment |
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
add a comment |
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}"
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 >> outputwith the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50
@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49
add a comment |
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}"
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 >> outputwith the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50
@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49
add a comment |
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}"
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 >> outputwith the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50
@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49
add a comment |
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}"
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}"
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 >> outputwith the output redirection outside the loop.
– glenn jackman
Nov 21 at 19:50
@CharlesDuffy updated, thx~!
– Mike Q
Nov 21 at 20:49
add a comment |
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 >> outputwith 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
add a comment |
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.
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%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
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
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