6

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.

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
LOGANr18
  • 61
  • 3
  • 2
    I don't know what's more bizarre- a question about cowsay, or that their is a tag for it. – Dan Brown Oct 02 '16 at 23:10
  • @DanBrown `cowsay` is a quite popular software, sure there would be a tag for it – Sergiy Kolodyazhnyy Oct 02 '16 at 23:15
  • 1
    @Serg Well yeah, but it's cowsay! The cow is useful, sure, and using a combination of packages to have a cow read your fortune on login is fun, sure, but really? (Rant over lol) – Dan Brown Oct 02 '16 at 23:16
  • 1
    @DanBrown there's a tag for `cat` ,too. Which doesn't do a lot as well, just prints out contents of a file. All fair and square :) – Sergiy Kolodyazhnyy Oct 02 '16 at 23:18
  • @Serg True, though cat is useful in android terminals. Meh, you can make anything useful (starts going through the little tidbits of packages I have to make me smile) – Dan Brown Oct 02 '16 at 23:20
  • 1
    @DanBrown. So I'm not the only one who has a cow read my fortune on login? Not always a cow, actually: `fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf -n1)`. – TRiG Oct 03 '16 at 10:31
  • @TRiG Nice. You can have the gold star. – Dan Brown Oct 03 '16 at 11:48
  • @Serg `cat`, as could be inferred from its name, is for con**cat**enating files, not simply for printing them. It has many programmatic uses, which `cowsay` does not, and it's part of the POSIX standard because of its importance as a command-line tool. I suppose this is AskUbuntu and therefore not targeted at developers, so it's reasonable to have a `cowsay` tag, but the comparison to `cat` is...a stretch. – Kyle Strand Oct 03 '16 at 14:47
  • @KyleStrand AskUbuntu is targeted at both developers and regular users. If you look up, there's tags for development as well. `cat` example was indeed oversimplified but there's plenty of useless uses of cat. Lets take another example - firefox or google chrome. There's tags for those yet they're not POSIX standard. What I'm saying is that for tag to exist, it doesn't have to be a standard application, it just has to be popular enough and useful to users. – Sergiy Kolodyazhnyy Oct 03 '16 at 15:47
  • Still, cat is becomes useful only in combination with other tools, IMHO. I've used it here-document form to print usage info in my scripts. It doesn't give end user that much aside from catenation and printing. – Sergiy Kolodyazhnyy Oct 03 '16 at 15:51
  • @Serg I meant not *specifically* targeted at devs, as opposed to SO. The whole point of mentioning the target audience was to acknowledge that a `cowsay` tag *is* appropriate here, whereas it would *not* be appropriate on e.g. SO. And although I mentioned POSIX standardization, nothing I said should have been taken to imply that *every tag on this site* must be a POSIX-standardized application!! As for `cat` "only" being useful in combination with other tools, (1) that's the whole point of the UNIX philosophy, and (2) how else would you view files prior to ubiquitous text editors? – Kyle Strand Oct 03 '16 at 16:02
  • @KyleStrand Well, i guess we can agree on that :) . As for viewing the file, back in the day before cat, there either were tools for printing file line by line or you had to write your own tool. It's a trivial and common exercise in all programmming languages – Sergiy Kolodyazhnyy Oct 03 '16 at 16:14

3 Answers3

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
  • 131,985
  • 21
  • 239
  • 326
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
  • 39,774
  • 7
  • 116
  • 176
  • 2
    If someone asked me five minutes ago "What's a cowsay?" I would have answered "mooo". I see how woefully ignorant I was five minutes ago... LOL. – WinEunuuchs2Unix Oct 02 '16 at 23:52
  • @WinEunuuchs2Unix It's a fun command line application a lot like `sl` is as well. =) – Terrance Oct 02 '16 at 23:54
  • Oh... and what does a cow say about `sl`? perhaps the two can alternate words in your script :). I do agree it's a fun CLI program and I'm happy I got to see it in action here. – WinEunuuchs2Unix Oct 02 '16 at 23:58
  • @WinEunuuchs2Unix LOL! `sl` is actually Steam Locomotive that just runs a train across the screen. The rumor is that they came up with it because so many people accidentally type in `sl` instead of `ls`. But yes, these are all just fun CLI programs. =) – Terrance Oct 03 '16 at 00:01
  • If the Steam Locomotive has a cow catcher in front then.... – WinEunuuchs2Unix Oct 03 '16 at 00:02
  • @WinEunuuchs2Unix LOL! I love it! It should have one! :D – Terrance Oct 03 '16 at 00:03
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
  • 103,293
  • 19
  • 273
  • 492