Printing one line at a time and replacing each iteration attempt to match the equivalent in words [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have file called file.txt
contain the following lines:
abc
bga
sre
I would like to print the lines in file.txt
one by one. The expected output should be as follows:
first gud = abc
second gud= bga
third gud = sre
fouth gud = abc
fifth gud= bga
sixth gud = sre
seventh gud = abc
eighth gud= bga
ninth gud = sre
tenth gud = abc
I used this code, but it did not work:
gud()
get=$(cat file.txt)
echo $get
for i in 1..10
do
gud
done
What I am doing wrong?
linux bash bash-functions
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
closed as unclear what you're asking by Jeff Schaller, Andy Dalton, Filipe Brandenburger, Kusalananda, don_crissti Sep 5 at 20:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
 |Â
show 6 more comments
up vote
2
down vote
favorite
I have file called file.txt
contain the following lines:
abc
bga
sre
I would like to print the lines in file.txt
one by one. The expected output should be as follows:
first gud = abc
second gud= bga
third gud = sre
fouth gud = abc
fifth gud= bga
sixth gud = sre
seventh gud = abc
eighth gud= bga
ninth gud = sre
tenth gud = abc
I used this code, but it did not work:
gud()
get=$(cat file.txt)
echo $get
for i in 1..10
do
gud
done
What I am doing wrong?
linux bash bash-functions
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
closed as unclear what you're asking by Jeff Schaller, Andy Dalton, Filipe Brandenburger, Kusalananda, don_crissti Sep 5 at 20:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Do yo umean you want to print the letters inabc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!
– Goro
Sep 5 at 15:34
first gud = abc
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
third gud = sre
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40
 |Â
show 6 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have file called file.txt
contain the following lines:
abc
bga
sre
I would like to print the lines in file.txt
one by one. The expected output should be as follows:
first gud = abc
second gud= bga
third gud = sre
fouth gud = abc
fifth gud= bga
sixth gud = sre
seventh gud = abc
eighth gud= bga
ninth gud = sre
tenth gud = abc
I used this code, but it did not work:
gud()
get=$(cat file.txt)
echo $get
for i in 1..10
do
gud
done
What I am doing wrong?
linux bash bash-functions
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have file called file.txt
contain the following lines:
abc
bga
sre
I would like to print the lines in file.txt
one by one. The expected output should be as follows:
first gud = abc
second gud= bga
third gud = sre
fouth gud = abc
fifth gud= bga
sixth gud = sre
seventh gud = abc
eighth gud= bga
ninth gud = sre
tenth gud = abc
I used this code, but it did not work:
gud()
get=$(cat file.txt)
echo $get
for i in 1..10
do
gud
done
What I am doing wrong?
linux bash bash-functions
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Sep 5 at 20:35
Goro
1,34641642
1,34641642
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Sep 5 at 15:33
Guzz
162
162
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Guzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
closed as unclear what you're asking by Jeff Schaller, Andy Dalton, Filipe Brandenburger, Kusalananda, don_crissti Sep 5 at 20:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Jeff Schaller, Andy Dalton, Filipe Brandenburger, Kusalananda, don_crissti Sep 5 at 20:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Do yo umean you want to print the letters inabc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!
– Goro
Sep 5 at 15:34
first gud = abc
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
third gud = sre
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40
 |Â
show 6 more comments
Do yo umean you want to print the letters inabc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!
– Goro
Sep 5 at 15:34
first gud = abc
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
third gud = sre
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40
Do yo umean you want to print the letters in
abc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!– Goro
Sep 5 at 15:34
Do yo umean you want to print the letters in
abc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!– Goro
Sep 5 at 15:34
first gud = abc
– Guzz
Sep 5 at 15:39
first gud = abc
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
third gud = sre
– Guzz
Sep 5 at 15:40
third gud = sre
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40
 |Â
show 6 more comments
3 Answers
3
active
oldest
votes
up vote
2
down vote
I see you need to read the input file in rotate as long as with the number of times you have had specified.
l=$(wc -l <infile)
for i in 1..10; do
sed -n "$( (($i % l == 0 )) && sed -n '$=' <infile || echo $(($i % l)) )"p <infile;
done
This will gives you a result as you'r expecting:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
(BTW, it's quite slow)
first gud =
second gud =
.... etc is missing from this output. No?
– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
add a comment |Â
up vote
2
down vote
This might be a slightly better and faster approach.
Create a script readAndRotate.sh:
#!/bin/bash
declare -a hmm
hmm=(`cat $1`)
numElem=$#hmm[@]
for i in 0..9
do
echo "$hmm[ $(( $i % $numElem )) ]"
done
Then run it like this:
readAndRotate.sh file.txt
and the output looks like:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
This code will iterate between the three lines in file.txt
as follows:
#!/bin/bash
for x in 1..3; do
while ((i++)); read -r line # ++ is post increment, increments value by 1
do
# get one number at a time
digit=$(echo $i )
# Case control structure to replace digit equivalent in words
case $digit in
1) echo -n "First ";;
2) echo -n "Second ";;
3) echo -n "Third ";;
4) echo -n "Fourth ";;
5) echo -n "Fifth ";;
6) echo -n "Sixth ";;
7) echo -n "Seventh ";;
8) echo -n "Eighth ";;
9) echo -n "Ninth ";;
10) echo -n "Tenth ";;
11) echo -n "Eleventh ";;
esac
echo $digit"gud""= $line" | tr -d '0123456789'
done < file.txt
done
The output is as follows:
First gud= abc
Second gud= bga
Third gud= sre
Fifth gud= abc
Sixth gud= bga
Seventh gud= sre
Nineth gud= abc
Tenth gud= bga
Eleventh gud= sre
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
I see you need to read the input file in rotate as long as with the number of times you have had specified.
l=$(wc -l <infile)
for i in 1..10; do
sed -n "$( (($i % l == 0 )) && sed -n '$=' <infile || echo $(($i % l)) )"p <infile;
done
This will gives you a result as you'r expecting:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
(BTW, it's quite slow)
first gud =
second gud =
.... etc is missing from this output. No?
– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
add a comment |Â
up vote
2
down vote
I see you need to read the input file in rotate as long as with the number of times you have had specified.
l=$(wc -l <infile)
for i in 1..10; do
sed -n "$( (($i % l == 0 )) && sed -n '$=' <infile || echo $(($i % l)) )"p <infile;
done
This will gives you a result as you'r expecting:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
(BTW, it's quite slow)
first gud =
second gud =
.... etc is missing from this output. No?
– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I see you need to read the input file in rotate as long as with the number of times you have had specified.
l=$(wc -l <infile)
for i in 1..10; do
sed -n "$( (($i % l == 0 )) && sed -n '$=' <infile || echo $(($i % l)) )"p <infile;
done
This will gives you a result as you'r expecting:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
(BTW, it's quite slow)
I see you need to read the input file in rotate as long as with the number of times you have had specified.
l=$(wc -l <infile)
for i in 1..10; do
sed -n "$( (($i % l == 0 )) && sed -n '$=' <infile || echo $(($i % l)) )"p <infile;
done
This will gives you a result as you'r expecting:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
(BTW, it's quite slow)
answered Sep 5 at 17:36
αғsýιη
15.6k92563
15.6k92563
first gud =
second gud =
.... etc is missing from this output. No?
– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
add a comment |Â
first gud =
second gud =
.... etc is missing from this output. No?
– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
first gud =
second gud =
.... etc is missing from this output. No?– Goro
Sep 5 at 17:53
first gud =
second gud =
.... etc is missing from this output. No?– Goro
Sep 5 at 17:53
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
current Edit is not from OP and I assume OP's feedback in comments are just assumptions to explain the requirements while h(is|her) code is saying more what probably s/he wants.
– Î±Ò“sýιη
Sep 5 at 18:02
add a comment |Â
up vote
2
down vote
This might be a slightly better and faster approach.
Create a script readAndRotate.sh:
#!/bin/bash
declare -a hmm
hmm=(`cat $1`)
numElem=$#hmm[@]
for i in 0..9
do
echo "$hmm[ $(( $i % $numElem )) ]"
done
Then run it like this:
readAndRotate.sh file.txt
and the output looks like:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
This might be a slightly better and faster approach.
Create a script readAndRotate.sh:
#!/bin/bash
declare -a hmm
hmm=(`cat $1`)
numElem=$#hmm[@]
for i in 0..9
do
echo "$hmm[ $(( $i % $numElem )) ]"
done
Then run it like this:
readAndRotate.sh file.txt
and the output looks like:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This might be a slightly better and faster approach.
Create a script readAndRotate.sh:
#!/bin/bash
declare -a hmm
hmm=(`cat $1`)
numElem=$#hmm[@]
for i in 0..9
do
echo "$hmm[ $(( $i % $numElem )) ]"
done
Then run it like this:
readAndRotate.sh file.txt
and the output looks like:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This might be a slightly better and faster approach.
Create a script readAndRotate.sh:
#!/bin/bash
declare -a hmm
hmm=(`cat $1`)
numElem=$#hmm[@]
for i in 0..9
do
echo "$hmm[ $(( $i % $numElem )) ]"
done
Then run it like this:
readAndRotate.sh file.txt
and the output looks like:
abc
bga
sre
abc
bga
sre
abc
bga
sre
abc
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Sep 5 at 18:02


B.McCready
211
211
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
B.McCready is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
up vote
2
down vote
This code will iterate between the three lines in file.txt
as follows:
#!/bin/bash
for x in 1..3; do
while ((i++)); read -r line # ++ is post increment, increments value by 1
do
# get one number at a time
digit=$(echo $i )
# Case control structure to replace digit equivalent in words
case $digit in
1) echo -n "First ";;
2) echo -n "Second ";;
3) echo -n "Third ";;
4) echo -n "Fourth ";;
5) echo -n "Fifth ";;
6) echo -n "Sixth ";;
7) echo -n "Seventh ";;
8) echo -n "Eighth ";;
9) echo -n "Ninth ";;
10) echo -n "Tenth ";;
11) echo -n "Eleventh ";;
esac
echo $digit"gud""= $line" | tr -d '0123456789'
done < file.txt
done
The output is as follows:
First gud= abc
Second gud= bga
Third gud= sre
Fifth gud= abc
Sixth gud= bga
Seventh gud= sre
Nineth gud= abc
Tenth gud= bga
Eleventh gud= sre
add a comment |Â
up vote
2
down vote
This code will iterate between the three lines in file.txt
as follows:
#!/bin/bash
for x in 1..3; do
while ((i++)); read -r line # ++ is post increment, increments value by 1
do
# get one number at a time
digit=$(echo $i )
# Case control structure to replace digit equivalent in words
case $digit in
1) echo -n "First ";;
2) echo -n "Second ";;
3) echo -n "Third ";;
4) echo -n "Fourth ";;
5) echo -n "Fifth ";;
6) echo -n "Sixth ";;
7) echo -n "Seventh ";;
8) echo -n "Eighth ";;
9) echo -n "Ninth ";;
10) echo -n "Tenth ";;
11) echo -n "Eleventh ";;
esac
echo $digit"gud""= $line" | tr -d '0123456789'
done < file.txt
done
The output is as follows:
First gud= abc
Second gud= bga
Third gud= sre
Fifth gud= abc
Sixth gud= bga
Seventh gud= sre
Nineth gud= abc
Tenth gud= bga
Eleventh gud= sre
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This code will iterate between the three lines in file.txt
as follows:
#!/bin/bash
for x in 1..3; do
while ((i++)); read -r line # ++ is post increment, increments value by 1
do
# get one number at a time
digit=$(echo $i )
# Case control structure to replace digit equivalent in words
case $digit in
1) echo -n "First ";;
2) echo -n "Second ";;
3) echo -n "Third ";;
4) echo -n "Fourth ";;
5) echo -n "Fifth ";;
6) echo -n "Sixth ";;
7) echo -n "Seventh ";;
8) echo -n "Eighth ";;
9) echo -n "Ninth ";;
10) echo -n "Tenth ";;
11) echo -n "Eleventh ";;
esac
echo $digit"gud""= $line" | tr -d '0123456789'
done < file.txt
done
The output is as follows:
First gud= abc
Second gud= bga
Third gud= sre
Fifth gud= abc
Sixth gud= bga
Seventh gud= sre
Nineth gud= abc
Tenth gud= bga
Eleventh gud= sre
This code will iterate between the three lines in file.txt
as follows:
#!/bin/bash
for x in 1..3; do
while ((i++)); read -r line # ++ is post increment, increments value by 1
do
# get one number at a time
digit=$(echo $i )
# Case control structure to replace digit equivalent in words
case $digit in
1) echo -n "First ";;
2) echo -n "Second ";;
3) echo -n "Third ";;
4) echo -n "Fourth ";;
5) echo -n "Fifth ";;
6) echo -n "Sixth ";;
7) echo -n "Seventh ";;
8) echo -n "Eighth ";;
9) echo -n "Ninth ";;
10) echo -n "Tenth ";;
11) echo -n "Eleventh ";;
esac
echo $digit"gud""= $line" | tr -d '0123456789'
done < file.txt
done
The output is as follows:
First gud= abc
Second gud= bga
Third gud= sre
Fifth gud= abc
Sixth gud= bga
Seventh gud= sre
Nineth gud= abc
Tenth gud= bga
Eleventh gud= sre
edited Sep 5 at 19:33
answered Sep 5 at 18:08
Goro
1,34641642
1,34641642
add a comment |Â
add a comment |Â
Do yo umean you want to print the letters in
abc bga sre
one by one? Would you please clarify the question overal. Not clear at all!!!– Goro
Sep 5 at 15:34
first gud = abc
– Guzz
Sep 5 at 15:39
second gud= bga
– Guzz
Sep 5 at 15:39
third gud = sre
– Guzz
Sep 5 at 15:40
sorry i using mobile
– Guzz
Sep 5 at 15:40