Reading from a text file and printing the words character by character at a time
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I would like to write a program that reads from a text file and prints in Terminal
the words of that file character by character every one second.
For example, in a text file log.txt
let's say I have this sentence:
I love Unix but I don't know programming.
I would like the code to read the previous sentence and print the letters, the spaces one by one every second.
shell text-processing
New contributor
Zahi 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
5
down vote
favorite
I would like to write a program that reads from a text file and prints in Terminal
the words of that file character by character every one second.
For example, in a text file log.txt
let's say I have this sentence:
I love Unix but I don't know programming.
I would like the code to read the previous sentence and print the letters, the spaces one by one every second.
shell text-processing
New contributor
Zahi 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
5
down vote
favorite
up vote
5
down vote
favorite
I would like to write a program that reads from a text file and prints in Terminal
the words of that file character by character every one second.
For example, in a text file log.txt
let's say I have this sentence:
I love Unix but I don't know programming.
I would like the code to read the previous sentence and print the letters, the spaces one by one every second.
shell text-processing
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I would like to write a program that reads from a text file and prints in Terminal
the words of that file character by character every one second.
For example, in a text file log.txt
let's say I have this sentence:
I love Unix but I don't know programming.
I would like the code to read the previous sentence and print the letters, the spaces one by one every second.
shell text-processing
shell text-processing
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Sep 8 at 21:15
don_crissti
47k15124154
47k15124154
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Sep 8 at 20:07
Zahi
636
636
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Zahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Zahi 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 |Â
5 Answers
5
active
oldest
votes
up vote
6
down vote
accepted
You can do this
var=$(cat log.txt)
for (( i=0; i<$#var; i++ )); do
sleep 1 | echo -ne "$var:$i:1"
done
echo ""
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
add a comment |Â
up vote
9
down vote
Goro's answer will work, but it should be noted that command substitution removes trailing newlines as specified by POSIX standard. Thus it may not be desirable where you want to actually iterate over all charactes, even non-printable ones. Another issue is that C-style for loop is used in bash and ksh93, but not in standard (aka POSIX-comliant ) /bin/sh
. The $variable:index:offset
form of parameter expansion is also type of bashism and not specified by POSIX definitions of parameter expansion (though supported by ksh93
and zsh
).
Nonetheless, there's a way to iterate over all characters in file portably and in a far more practical way. That's to use awk
:
# all characters on the same line
$ awk 'for(i=1;i<=length;i++) printf "%c",substr($0,i,1); system("sleep 1");; print' input.txt
# all characters on separate lines
$ awk 'for(i=1;i<=length;i++) print substr($0, i, 1); system("sleep 1"); ' input.txt
With this command substr()
and system()
are both specified in POSIX awk and will in fact iterate over all characters.
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
@don_crissti I need to find a better phrase than "bashism" because I always getksh
andzsh
mentioned. Can we start using something likeba,k,zsh
maybe ? :)
– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based onksh
and Bourne shell, and bash has--posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it
– Sergiy Kolodyazhnyy
Sep 9 at 0:28
 |Â
show 1 more comment
up vote
3
down vote
With bash
or ksh93
you can read single characters using the shell's built-in read
command:
while IFS= read -r -n 1 c; do
printf '%c' "$c"
sleep 1
done < log.txt
printf 'n'
add a comment |Â
up vote
1
down vote
Here's a Python solution:
python -c "from time import sleep
with open('/tmp/file.txt') as f:
for line in f:
for c in line:
print(c, end='', flush=True);sleep(1);"
You should just be able to paste that on the command line and change the name of the input file.
add a comment |Â
up vote
0
down vote
You could do this with Perl
as shown:
perl -pe 'BEGIN=1; sleep 1' log.txt
-p
will autoprint the current record before fetching the next.$/=1
will makeperl
read the input stream a byte at a time, and assuming the characters are a byte sized.$|=1
will output is buffered.
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You can do this
var=$(cat log.txt)
for (( i=0; i<$#var; i++ )); do
sleep 1 | echo -ne "$var:$i:1"
done
echo ""
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
add a comment |Â
up vote
6
down vote
accepted
You can do this
var=$(cat log.txt)
for (( i=0; i<$#var; i++ )); do
sleep 1 | echo -ne "$var:$i:1"
done
echo ""
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You can do this
var=$(cat log.txt)
for (( i=0; i<$#var; i++ )); do
sleep 1 | echo -ne "$var:$i:1"
done
echo ""
You can do this
var=$(cat log.txt)
for (( i=0; i<$#var; i++ )); do
sleep 1 | echo -ne "$var:$i:1"
done
echo ""
edited Sep 8 at 21:08
answered Sep 8 at 20:10
Goro
1,40641643
1,40641643
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
add a comment |Â
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
thk for quick answr. this is wht i want but why it priunt on the same line ike this `I love Unix but I don't know programming.[zahi@sonar]$, how print on separate line?
– Zahi
Sep 8 at 20:30
Ah, please see my revisions
– Goro
Sep 8 at 20:32
Ah, please see my revisions
– Goro
Sep 8 at 20:32
thank you very much
– Zahi
Sep 8 at 20:34
thank you very much
– Zahi
Sep 8 at 20:34
add a comment |Â
up vote
9
down vote
Goro's answer will work, but it should be noted that command substitution removes trailing newlines as specified by POSIX standard. Thus it may not be desirable where you want to actually iterate over all charactes, even non-printable ones. Another issue is that C-style for loop is used in bash and ksh93, but not in standard (aka POSIX-comliant ) /bin/sh
. The $variable:index:offset
form of parameter expansion is also type of bashism and not specified by POSIX definitions of parameter expansion (though supported by ksh93
and zsh
).
Nonetheless, there's a way to iterate over all characters in file portably and in a far more practical way. That's to use awk
:
# all characters on the same line
$ awk 'for(i=1;i<=length;i++) printf "%c",substr($0,i,1); system("sleep 1");; print' input.txt
# all characters on separate lines
$ awk 'for(i=1;i<=length;i++) print substr($0, i, 1); system("sleep 1"); ' input.txt
With this command substr()
and system()
are both specified in POSIX awk and will in fact iterate over all characters.
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
@don_crissti I need to find a better phrase than "bashism" because I always getksh
andzsh
mentioned. Can we start using something likeba,k,zsh
maybe ? :)
– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based onksh
and Bourne shell, and bash has--posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it
– Sergiy Kolodyazhnyy
Sep 9 at 0:28
 |Â
show 1 more comment
up vote
9
down vote
Goro's answer will work, but it should be noted that command substitution removes trailing newlines as specified by POSIX standard. Thus it may not be desirable where you want to actually iterate over all charactes, even non-printable ones. Another issue is that C-style for loop is used in bash and ksh93, but not in standard (aka POSIX-comliant ) /bin/sh
. The $variable:index:offset
form of parameter expansion is also type of bashism and not specified by POSIX definitions of parameter expansion (though supported by ksh93
and zsh
).
Nonetheless, there's a way to iterate over all characters in file portably and in a far more practical way. That's to use awk
:
# all characters on the same line
$ awk 'for(i=1;i<=length;i++) printf "%c",substr($0,i,1); system("sleep 1");; print' input.txt
# all characters on separate lines
$ awk 'for(i=1;i<=length;i++) print substr($0, i, 1); system("sleep 1"); ' input.txt
With this command substr()
and system()
are both specified in POSIX awk and will in fact iterate over all characters.
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
@don_crissti I need to find a better phrase than "bashism" because I always getksh
andzsh
mentioned. Can we start using something likeba,k,zsh
maybe ? :)
– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based onksh
and Bourne shell, and bash has--posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it
– Sergiy Kolodyazhnyy
Sep 9 at 0:28
 |Â
show 1 more comment
up vote
9
down vote
up vote
9
down vote
Goro's answer will work, but it should be noted that command substitution removes trailing newlines as specified by POSIX standard. Thus it may not be desirable where you want to actually iterate over all charactes, even non-printable ones. Another issue is that C-style for loop is used in bash and ksh93, but not in standard (aka POSIX-comliant ) /bin/sh
. The $variable:index:offset
form of parameter expansion is also type of bashism and not specified by POSIX definitions of parameter expansion (though supported by ksh93
and zsh
).
Nonetheless, there's a way to iterate over all characters in file portably and in a far more practical way. That's to use awk
:
# all characters on the same line
$ awk 'for(i=1;i<=length;i++) printf "%c",substr($0,i,1); system("sleep 1");; print' input.txt
# all characters on separate lines
$ awk 'for(i=1;i<=length;i++) print substr($0, i, 1); system("sleep 1"); ' input.txt
With this command substr()
and system()
are both specified in POSIX awk and will in fact iterate over all characters.
Goro's answer will work, but it should be noted that command substitution removes trailing newlines as specified by POSIX standard. Thus it may not be desirable where you want to actually iterate over all charactes, even non-printable ones. Another issue is that C-style for loop is used in bash and ksh93, but not in standard (aka POSIX-comliant ) /bin/sh
. The $variable:index:offset
form of parameter expansion is also type of bashism and not specified by POSIX definitions of parameter expansion (though supported by ksh93
and zsh
).
Nonetheless, there's a way to iterate over all characters in file portably and in a far more practical way. That's to use awk
:
# all characters on the same line
$ awk 'for(i=1;i<=length;i++) printf "%c",substr($0,i,1); system("sleep 1");; print' input.txt
# all characters on separate lines
$ awk 'for(i=1;i<=length;i++) print substr($0, i, 1); system("sleep 1"); ' input.txt
With this command substr()
and system()
are both specified in POSIX awk and will in fact iterate over all characters.
edited Sep 8 at 21:33
answered Sep 8 at 21:12


Sergiy Kolodyazhnyy
7,92011648
7,92011648
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
@don_crissti I need to find a better phrase than "bashism" because I always getksh
andzsh
mentioned. Can we start using something likeba,k,zsh
maybe ? :)
– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based onksh
and Bourne shell, and bash has--posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it
– Sergiy Kolodyazhnyy
Sep 9 at 0:28
 |Â
show 1 more comment
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
@don_crissti I need to find a better phrase than "bashism" because I always getksh
andzsh
mentioned. Can we start using something likeba,k,zsh
maybe ? :)
– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based onksh
and Bourne shell, and bash has--posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it
– Sergiy Kolodyazhnyy
Sep 9 at 0:28
1
1
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
@Goro Already added a comment about that, see the edit
– Sergiy Kolodyazhnyy
Sep 8 at 21:15
3
3
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
Very elegant!! thanks! ;-)
– Goro
Sep 8 at 21:20
2
2
@don_crissti I need to find a better phrase than "bashism" because I always get
ksh
and zsh
mentioned. Can we start using something like ba,k,zsh
maybe ? :)– Sergiy Kolodyazhnyy
Sep 8 at 21:33
@don_crissti I need to find a better phrase than "bashism" because I always get
ksh
and zsh
mentioned. Can we start using something like ba,k,zsh
maybe ? :)– Sergiy Kolodyazhnyy
Sep 8 at 21:33
2
2
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
I think just mentioning non-posix or non-standard would do...
– don_crissti
Sep 8 at 21:33
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based on
ksh
and Bourne shell, and bash has --posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it– Sergiy Kolodyazhnyy
Sep 9 at 0:28
@don_crissti non-posix adds another level of ambiguity because POSIX mostly was based on
ksh
and Bourne shell, and bash has --posix
flag and supoorts most of the POSIX features. Idk, there's gotta be a better way to phrase it– Sergiy Kolodyazhnyy
Sep 9 at 0:28
 |Â
show 1 more comment
up vote
3
down vote
With bash
or ksh93
you can read single characters using the shell's built-in read
command:
while IFS= read -r -n 1 c; do
printf '%c' "$c"
sleep 1
done < log.txt
printf 'n'
add a comment |Â
up vote
3
down vote
With bash
or ksh93
you can read single characters using the shell's built-in read
command:
while IFS= read -r -n 1 c; do
printf '%c' "$c"
sleep 1
done < log.txt
printf 'n'
add a comment |Â
up vote
3
down vote
up vote
3
down vote
With bash
or ksh93
you can read single characters using the shell's built-in read
command:
while IFS= read -r -n 1 c; do
printf '%c' "$c"
sleep 1
done < log.txt
printf 'n'
With bash
or ksh93
you can read single characters using the shell's built-in read
command:
while IFS= read -r -n 1 c; do
printf '%c' "$c"
sleep 1
done < log.txt
printf 'n'
answered Sep 8 at 22:46
steeldriver
32.1k34979
32.1k34979
add a comment |Â
add a comment |Â
up vote
1
down vote
Here's a Python solution:
python -c "from time import sleep
with open('/tmp/file.txt') as f:
for line in f:
for c in line:
print(c, end='', flush=True);sleep(1);"
You should just be able to paste that on the command line and change the name of the input file.
add a comment |Â
up vote
1
down vote
Here's a Python solution:
python -c "from time import sleep
with open('/tmp/file.txt') as f:
for line in f:
for c in line:
print(c, end='', flush=True);sleep(1);"
You should just be able to paste that on the command line and change the name of the input file.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here's a Python solution:
python -c "from time import sleep
with open('/tmp/file.txt') as f:
for line in f:
for c in line:
print(c, end='', flush=True);sleep(1);"
You should just be able to paste that on the command line and change the name of the input file.
Here's a Python solution:
python -c "from time import sleep
with open('/tmp/file.txt') as f:
for line in f:
for c in line:
print(c, end='', flush=True);sleep(1);"
You should just be able to paste that on the command line and change the name of the input file.
answered Sep 9 at 3:32
user1717828
1,60611125
1,60611125
add a comment |Â
add a comment |Â
up vote
0
down vote
You could do this with Perl
as shown:
perl -pe 'BEGIN=1; sleep 1' log.txt
-p
will autoprint the current record before fetching the next.$/=1
will makeperl
read the input stream a byte at a time, and assuming the characters are a byte sized.$|=1
will output is buffered.
add a comment |Â
up vote
0
down vote
You could do this with Perl
as shown:
perl -pe 'BEGIN=1; sleep 1' log.txt
-p
will autoprint the current record before fetching the next.$/=1
will makeperl
read the input stream a byte at a time, and assuming the characters are a byte sized.$|=1
will output is buffered.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You could do this with Perl
as shown:
perl -pe 'BEGIN=1; sleep 1' log.txt
-p
will autoprint the current record before fetching the next.$/=1
will makeperl
read the input stream a byte at a time, and assuming the characters are a byte sized.$|=1
will output is buffered.
You could do this with Perl
as shown:
perl -pe 'BEGIN=1; sleep 1' log.txt
-p
will autoprint the current record before fetching the next.$/=1
will makeperl
read the input stream a byte at a time, and assuming the characters are a byte sized.$|=1
will output is buffered.
answered 2 days ago
Rakesh Sharma
53013
53013
add a comment |Â
add a comment |Â
Zahi is a new contributor. Be nice, and check out our Code of Conduct.
Zahi is a new contributor. Be nice, and check out our Code of Conduct.
Zahi is a new contributor. Be nice, and check out our Code of Conduct.
Zahi is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467767%2freading-from-a-text-file-and-printing-the-words-character-by-character-at-a-time%23new-answer', 'question_page');
);
Post as a guest
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
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
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