嗨,我只是想知道如何cowsay
从文本文件中一次读取一个单词。我现在处于零基础状态,使用 putty,真的需要帮助。
答案1
这似乎是一个罕见的情况,实际上需要进行单词拆分:
for word in $(<file.txt); do cowsay "$word"; sleep 1; done
(该sleep
命令是可选的)。或者总是有xargs
:
xargs -a file.txt -n1 cowsay
答案2
这是我很快想到的办法。我在测试文件中放入一行,然后将其输入到 cowsay。
terrance@terrance-ubuntu:~$ cat cstest.txt
This is a test file to test cowsay
我将其设置为读取每一行,然后for loop
对每一行执行一次以读取每个单词。示例如下:
:~$ cat cstest.txt | while read line; do for word in $line; do cowsay $word; done; done
______
< This >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
____
< is >
----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
___
< a >
---
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
______
< test >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
______
< file >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
____
< to >
----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
______
< test >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
________
< cowsay >
--------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
该命令的每一行看起来如下:
:~$ cat cstest.txt | while read line
>do
>for word in $line
>do
>cowsay $word
>done
>done
希望这可以帮助!
答案3
Python单行代码:
python -c 'import sys,subprocess;[subprocess.call(["cowsay",w]) for l in sys.stdin for w in l.split()]' < words.txt
示例运行:
$ cat words.txt
this is a test
$ python -c 'import sys,subprocess;[subprocess.call(["cowsay",w]) for l in sys.stdin for w in l.split()]' < words.txt
______
< this >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
____
< is >
----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
___
< a >
---
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
______
< test >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
$