从文本文件中读取并一次逐字符地打印单词

时间:2022-09-13 09:53:27

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:

例如,在文本文件log.txt中,假设我有这样的句子:

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.

我希望代码能够读取前一句并逐字打印这些空格。

4 个解决方案

#1


6  

You can do this

你可以这样做

var=$(cat log.txt)
for (( i=0; i<${#var}; i++ )); do
 sleep 1 | echo -ne "${var:$i:1}"
done
echo ""

#2


8  

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

Goro的答案是有效的,但应该注意的是,命令替换会删除POSIX标准规定的尾随换行符。因此,您可能不希望实际迭代所有字符,甚至是不可打印的字符。另一个问题是C风格的for循环用于bash和ksh93,但不是标准的(也就是POSIX兼容)/ bin / sh。参数扩展的$ {variable:index:offset}形式也是bashism的类型,并未由参数扩展的POSIX定义指定(尽管由ksh93和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:

尽管如此,有一种方法可以以更实用的方式以可移植的方式迭代文件中的所有字符。那是用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.

使用此命令,substr()和system()都在POSIX awk中指定,并且实际上将迭代所有字符。

#3


3  

With bash or ksh93 you can read single characters using the shell's built-in read command:

使用bash或ksh93,您可以使用shell的内置read命令读取单个字符:

while IFS= read -r -n 1 c; do 
  printf '%c' "$c"
  sleep 1
done < log.txt
printf '\n'

#4


1  

Here's a Python solution:

这是一个Python解决方案:

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.

您应该只能将其粘贴到命令行并更改输入文件的名称。

#1


6  

You can do this

你可以这样做

var=$(cat log.txt)
for (( i=0; i<${#var}; i++ )); do
 sleep 1 | echo -ne "${var:$i:1}"
done
echo ""

#2


8  

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

Goro的答案是有效的,但应该注意的是,命令替换会删除POSIX标准规定的尾随换行符。因此,您可能不希望实际迭代所有字符,甚至是不可打印的字符。另一个问题是C风格的for循环用于bash和ksh93,但不是标准的(也就是POSIX兼容)/ bin / sh。参数扩展的$ {variable:index:offset}形式也是bashism的类型,并未由参数扩展的POSIX定义指定(尽管由ksh93和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:

尽管如此,有一种方法可以以更实用的方式以可移植的方式迭代文件中的所有字符。那是用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.

使用此命令,substr()和system()都在POSIX awk中指定,并且实际上将迭代所有字符。

#3


3  

With bash or ksh93 you can read single characters using the shell's built-in read command:

使用bash或ksh93,您可以使用shell的内置read命令读取单个字符:

while IFS= read -r -n 1 c; do 
  printf '%c' "$c"
  sleep 1
done < log.txt
printf '\n'

#4


1  

Here's a Python solution:

这是一个Python解决方案:

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.

您应该只能将其粘贴到命令行并更改输入文件的名称。