Hi just wondering how to get cowsay to read off one word at a time from a text file. I'm at ground zero right now, using putty and really need help.
Asked
Active
Viewed 1,396 times
6
Sergiy Kolodyazhnyy
- 107,582
LOGANr18
- 61
3 Answers
7
This seems to be a rare case where word splitting is actually desirable:
for word in $(<file.txt); do cowsay "$word"; sleep 1; done
(the sleep command is optional). Or there's always xargs:
xargs -a file.txt -n1 cowsay
steeldriver
- 142,475
5
Here's something I came up with really quickly. I put one line in a test file then fed it to cowsay.
terrance@terrance-ubuntu:~$ cat cstest.txt
This is a test file to test cowsay
I set it to read each line, then do a for loop of each line to read each word. Example below:
:~$ 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 |
|| ||
Each individual line of that command would look like:
:~$ cat cstest.txt | while read line
>do
>for word in $line
>do
>cowsay $word
>done
>done
Hope this helps!
Terrance
- 43,712
5
Python one-liner:
python -c 'import sys,subprocess;[subprocess.call(["cowsay",w]) for l in sys.stdin for w in l.split()]' < words.txt
Sample run:
$ 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 |
|| ||
$
Sergiy Kolodyazhnyy
- 107,582