LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2019, 03:35 PM   #1
battlestationX
LQ Newbie
 
Registered: Jun 2019
Posts: 16

Rep: Reputation: Disabled
How to output a random ASCII logo from a text file using a shell script?


I have a plain text file that contains ASCII logos seperated by white space.
Im trying to figure out how to make a script that can output a random logo from that file.. Basically what im trying to do is shuffle blocks of text in a file and output only one such block. For simplicity all ASCII logos will not contain new lines (each logo will be a single block of text)

The following is what I tried, it radomizes the blocks, but outputs the whole file.. I assume the command (which I dont understand, ust found it) processes the whole file at once, and thus why I cant print out individual array items.

Code:
#!/bin/bash
myArray=$(sed '1s/^/\n/; s/^$/\x00/' ascii_art.txt | shuf -z | sed '1d; s/\x00//')
echo "${myArray[0]}"
Here's what 'ascii_art.txt' contains (it actually has more logos though):

Code:
VVVVVVVV           VVVVVVVVIIIIIIIIIIMMMMMMMM               MMMMMMMM     
V::::::V           V::::::VI::::::::IM:::::::M             M:::::::M     
V::::::V           V::::::VI::::::::IM::::::::M           M::::::::M     
V::::::V           V::::::VII::::::IIM:::::::::M         M:::::::::M     
 V:::::V           V:::::V   I::::I  M::::::::::M       M::::::::::M     
  V:::::V         V:::::V    I::::I  M:::::::::::M     M:::::::::::M     
   V:::::V       V:::::V     I::::I  M:::::::M::::M   M::::M:::::::M     
    V:::::V     V:::::V      I::::I  M::::::M M::::M M::::M M::::::M     
     V:::::V   V:::::V       I::::I  M::::::M  M::::M::::M  M::::::M     
      V:::::V V:::::V        I::::I  M::::::M   M:::::::M   M::::::M     
       V:::::V:::::V         I::::I  M::::::M    M:::::M    M::::::M     
        V:::::::::V          I::::I  M::::::M     MMMMM     M::::::M     
         V:::::::V         II::::::IIM::::::M               M::::::M     
          V:::::V          I::::::::IM::::::M               M::::::M     
           V:::V           I::::::::IM::::::M               M::::::M     
            VVV            IIIIIIIIIIMMMMMMMM               MMMMMMMM     



 __     __  __                      __  __  __                                  __       __  ________    
/  |   /  |/  |                    /  |/  |/  |                                /  \     /  |/        |   
$$ |   $$ |$$/  _____  ____        $$ |$$/ $$ |   __   ______    _______       $$  \   /$$ |$$$$$$$$/    
$$ |   $$ |/  |/     \/    \       $$ |/  |$$ |  /  | /      \  /       |      $$$  \ /$$$ |$$ |__       
$$  \ /$$/ $$ |$$$$$$ $$$$  |      $$ |$$ |$$ |_/$$/ /$$$$$$  |/$$$$$$$/       $$$$  /$$$$ |$$    |      
 $$  /$$/  $$ |$$ | $$ | $$ |      $$ |$$ |$$   $$<  $$    $$ |$$      \       $$ $$ $$/$$ |$$$$$/       
  $$ $$/   $$ |$$ | $$ | $$ |      $$ |$$ |$$$$$$  \ $$$$$$$$/  $$$$$$  |      $$ |$$$/ $$ |$$ |_____    
   $$$/    $$ |$$ | $$ | $$ |      $$ |$$ |$$ | $$  |$$       |/     $$/       $$ | $/  $$ |$$       |   
    $/     $$/ $$/  $$/  $$/       $$/ $$/ $$/   $$/  $$$$$$$/ $$$$$$$/        $$/      $$/ $$$$$$$$/    


MN0kkkkkkkkkkkkkkdl.dKWKkkkkkkkkkkkkkKVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
Wo;looooooooool,;;;;;;;;loooooooooooolVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
Nc;lk0KKKKKKOkl;..;;;:;.lkO0KKKKKK0Oc.VIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
M0o,:OKKKKK0l:.;;;;;;;,..oO00KKK0Oo;,dVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
MMWo:OKKKKK0l;.;;;;:;..ckK00KKKOd;,oKMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVI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IMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
MMMNkooooONMWKo,......;xXMMMMMMMMMMMMMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
MMMMMMMMMMMMMMWKo,..;dXMMMMMMMMMMMMMMMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM
MMMMMMMMMMMMMMMMWO.dKMMMMMMMMMMMMMMMMMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIMVIM

Last edited by battlestationX; 07-20-2019 at 12:53 AM.
 
Old 07-20-2019, 07:47 AM   #2
individual
Member
 
Registered: Jul 2018
Posts: 231

Rep: Reputation: 175Reputation: 175
There's no need for an array if all you want to do is print a random logo.
Code:
cat -s logos.txt | sed 's/^$/\x00/g' | shuf -z -n 1 | tr -d '\0'
I actually just learned about the -s flag for cat. It removes consecutive blank lines, which you have separating your logos.
Shuf's -n flag also allows you to limit the number of "lines" printed after shuffling.
The tr at the end deletes the NULL byte.
 
1 members found this post helpful.
Old 07-20-2019, 08:00 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 8,982

Rep: Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875
did you know you can make up your own text art?
http://patorjk.com/software/taag/#p=...t=Arco%20Linux
 
Old 07-20-2019, 03:25 PM   #4
battlestationX
LQ Newbie
 
Registered: Jun 2019
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by individual View Post
There's no need for an array if all you want to do is print a random logo.
Code:
cat -s logos.txt | sed 's/^$/\x00/g' | shuf -z -n 1 | tr -d '\0'
I actually just learned about the -s flag for cat. It removes consecutive blank lines, which you have separating your logos.
Shuf's -n flag also allows you to limit the number of "lines" printed after shuffling.
The tr at the end deletes the NULL byte.
Thanks man I love you, you're a life saver.. I was afraid I was going to have to spend days trying to figure that out lol..
 
Old 07-20-2019, 03:26 PM   #5
battlestationX
LQ Newbie
 
Registered: Jun 2019
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
did you know you can make up your own text art?
http://patorjk.com/software/taag/#p=...t=Arco%20Linux
I know there's also figlet.. But i'm not an 'arteest' so im not going to even try.
 
Old 07-20-2019, 03:44 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 8,982

Rep: Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875Reputation: 1875
Quote:
Originally Posted by battlestationX View Post
I know there's also figlet.. But i'm not an 'arteest' so im not going to even try.
that one yu just type in what you want to say and it does it for you.
 
Old 07-20-2019, 06:00 PM   #7
battlestationX
LQ Newbie
 
Registered: Jun 2019
Posts: 16

Original Poster
Rep: Reputation: Disabled
^ Yeah but they dont look as good as the ones that are hand-crafted.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] ASCII to HEX and hex to ascii ////// Programming 17 05-08-2018 09:55 PM
How to Change /Create My Own Logo Instead of Linux logo pillisrikanth Linux - Software 1 02-25-2012 07:29 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 06:05 AM
Change Suse Logo to KDE Logo little_penguin SUSE / openSUSE 2 04-05-2005 05:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration