LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-16-2021, 10:48 PM   #1
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Rep: Reputation: Disabled
Create a story with randoms outputs in bash


I have created a story but I want to sub random words kinda like madlibs but without user inputs. I have the words listed in different txt files ie noun.txt or verb.txt but I do not know how to call it. I am assuming using grep with sort r.
 
Old 03-17-2021, 02:22 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Welcome to LQ and the Programming fourm!

It is difficult to understand how you want this to work from your short description.

Could you provide a short sample paragraph and a short sample of the noun and verb files, and then show an example of how you would expect substitutions to work based on those examples.

You mention user input, so please also describe how the user would be reading the "story" and whether the substitutions should be made globally on the story source once, or each time the user reads the story. Help others to understand what you are trying to accomplish!

Please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.

Good luck!
 
1 members found this post helpful.
Old 03-17-2021, 06:24 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
I want to sub random words kinda like madlibs but without user inputs. I have the words listed in different txt files ie noun.txt or verb.txt but I do not know how to call it.
You want to pull random words from a text file?

Example, step by step, so you can understand it.

Code:
#Make a text file
touch noun.txt

#Put animal names on separate lines in noun.txt
for i in cat dog bear cow pig horse rabbit bird fish deer; do
    echo "$i" >> noun.txt
done

#Count the lines
wc -l noun.txt
10 noun.txt

#Make an array out of the words in noun.txt
myvar=($(while read line; do echo "$line"; done < noun.txt))

#Make a random number from 0 to 9, because there are 10 lines
#echo a random word from noun.txt in a loop
while :; do
    ran=$((0 + RANDOM % 9))
    echo "${myvar[$ran]}"
    sleep .3
done
 
3 members found this post helpful.
Old 03-17-2021, 06:27 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Here is an example I created to test `shuf` and `readarray`:
Code:
#!/bin/bash

cat >nouns.txt <<DONE
snail
dog
house
fork
haystack
DONE

cat >verbs.txt <<DONE
dig
drink
bring
DONE

readarray -t Nouns < <(shuf -n3 nouns.txt)
readarray -t Verbs < <(shuf -n2 verbs.txt)

printf "${Verbs[0]} ${Nouns[0]}\n"
printf "${Verbs[1]} ${Nouns[1]}\n"
printf "${Verbs[0]} ${Nouns[2]}\n"
 
2 members found this post helpful.
Old 03-17-2021, 07:13 AM   #5
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
You want to pull random words from a text file?

Example, step by step, so you can understand it.

Code:
#Make a text file
touch noun.txt

#Put animal names on separate lines in noun.txt
for i in cat dog bear cow pig horse rabbit bird fish deer; do
    echo "$i" >> noun.txt
done

#Count the lines
wc -l noun.txt
10 noun.txt

#Make an array out of the words in noun.txt
myvar=($(while read line; do echo "$line"; done < noun.txt))

#Make a random number from 0 to 9, because there are 10 lines
#echo a random word from noun.txt in a loop
while :; do
    ran=$((0 + RANDOM % 9))
    echo "${myvar[$ran]}"
    sleep .3
done
Thank you but i want put the words in a story. Ex Mary had a little (noun) and her (adjective) was as (verb) as snow. but the output pulls a different word and gives a different story everytime its ran.

Last edited by Nicole2320; 03-17-2021 at 07:14 AM.
 
Old 03-17-2021, 07:27 AM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Then write yourself a script that does what you want, with the examples that you were given.

If you have trouble with your script, after you have written one, and it does not work rite, post the errors that you are getting, and where you are stuck.

Code:
var="cow"
echo "Mary had a little "$var""
 
3 members found this post helpful.
Old 03-17-2021, 09:01 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by Nicole2320 View Post
Ex Mary had a little (noun) and her (adjective) was as (verb) as snow.
The grammatical nomenclature in your example doesn't make sense to me. Should it be
Mary (verb) a little (noun) and her (noun) was as (adjective) as snow?
 
1 members found this post helpful.
Old 03-17-2021, 09:31 PM   #8
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Then write yourself a script that does what you want, with the examples that you were given.

If you have trouble with your script, after you have written one, and it does not work rite, post the errors that you are getting, and where you are stuck.

Code:
var="cow"
echo "Mary had a little "$var""
Thank you
 
Old 03-17-2021, 09:32 PM   #9
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
You want to pull random words from a text file?

Example, step by step, so you can understand it.

Code:
#Make a text file
touch noun.txt

#Put animal names on separate lines in noun.txt
for i in cat dog bear cow pig horse rabbit bird fish deer; do
    echo "$i" >> noun.txt
done

#Count the lines
wc -l noun.txt
10 noun.txt

#Make an array out of the words in noun.txt
myvar=($(while read line; do echo "$line"; done < noun.txt))

#Make a random number from 0 to 9, because there are 10 lines
#echo a random word from noun.txt in a loop
while :; do
    ran=$((0 + RANDOM % 9))
    echo "${myvar[$ran]}"
    sleep .3
done
thank you
 
Old 03-17-2021, 09:33 PM   #10
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
The grammatical nomenclature in your example doesn't make sense to me. Should it be
Mary (verb) a little (noun) and her (noun) was as (adjective) as snow?
I was not trying to be correct grammatically, it was just a quick example of how I wanted words to be sub into the story. What I gave is not the story I am working on. Thank you for the suggestion.
 
Old 03-17-2021, 09:41 PM   #11
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks everybody for the suggestions
 
Old 03-18-2021, 06:21 AM   #12
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Another simple example for the OP:

Code:
#Make arrays of nouns
noun1=(cow horse pig dog cat bird frog sheep owl elephant koala wombat)
noun2=(coat fur shoe hat nose tail leg ear back hair chin belly)
noun3=(soap clouds rice snow popcorn salt smoke powder cotton linen fog paper)

#Make sentence with nouns from arrays
for i in {0..11}; do
    echo "Mary had a little "${noun1[$i]}", whose "${noun2[$i]}" was white as "${noun3[$i]}""
done


#Make sentence with sudo random items from arrays
while true; do
    ran=$((0 + RANDOM % 11))
    echo "Mary has a "${noun1[$ran]}""
    echo "She really likes its "${noun2[$ran]}""
    echo "Her "${noun1[$ran]}" likes to eat "${noun3[$ran]}""
    echo -e ''$_{1..50}'\b─'
    sleep 5
done
 
Old 03-18-2021, 06:37 PM   #13
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Another simple example for the OP:

Code:
#Make arrays of nouns
noun1=(cow horse pig dog cat bird frog sheep owl elephant koala wombat)
noun2=(coat fur shoe hat nose tail leg ear back hair chin belly)
noun3=(soap clouds rice snow popcorn salt smoke powder cotton linen fog paper)

#Make sentence with nouns from arrays
for i in {0..11}; do
    echo "Mary had a little "${noun1[$i]}", whose "${noun2[$i]}" was white as "${noun3[$i]}""
done


#Make sentence with sudo random items from arrays
while true; do
    ran=$((0 + RANDOM % 11))
    echo "Mary has a "${noun1[$ran]}""
    echo "She really likes its "${noun2[$ran]}""
    echo "Her "${noun1[$ran]}" likes to eat "${noun3[$ran]}""
    echo -e ''$_{1..50}'\b─'
    sleep 5
done
yall are good at this, i was starting small
#!/bin/bash
filename='noun.txt'

while read -r line; do
# reading each line
echo "Mary had a $line"

done < $filename
 
1 members found this post helpful.
Old 03-19-2021, 02:24 PM   #14
Nicole2320
LQ Newbie
 
Registered: Mar 2021
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Another simple example for the OP:

Code:
#Make arrays of nouns
noun1=(cow horse pig dog cat bird frog sheep owl elephant koala wombat)
noun2=(coat fur shoe hat nose tail leg ear back hair chin belly)
noun3=(soap clouds rice snow popcorn salt smoke powder cotton linen fog paper)

#Make sentence with nouns from arrays
for i in {0..11}; do
    echo "Mary had a little "${noun1[$i]}", whose "${noun2[$i]}" was white as "${noun3[$i]}""
done


#Make sentence with sudo random items from arrays
while true; do
    ran=$((0 + RANDOM % 11))
    echo "Mary has a "${noun1[$ran]}""
    echo "She really likes its "${noun2[$ran]}""
    echo "Her "${noun1[$ran]}" likes to eat "${noun3[$ran]}""
    echo -e ''$_{1..50}'\b─'
    sleep 5
done


I thought I had it changing it to something i can understand. My issue now is i cant get the output to display to the screen and it is not creating the number of files i selected for input. I would say 5 but only 3 are generating
Code:
#!/bin/bash
noun1=(cow horse pig dog cat bird frog sheep owl elephant koala wombat)
noun2=(coat fur shoe hat nose tail leg ear back hair chin belly)
noun3=(soap clouds rice snow popcorn salt smoke powder cotton linen fog paper)
name=(coat fur shoe hat nose tail leg ear back hair chin belly)

read -p "How many stories would you like to see?: " M

for((i=1; i<=M; i++));
do
    echo "Mary had a little"${noun1[$RANDOM % ${#noun1[@]} ]}", Her "${noun2[$RANDOM % ${#noun2[@]} ]}" was "${noun3[$RANDOM % ${#noun3[@]} ]}" to me" >  ${name[$RANDOM % ${#name[@]} ]}.txt
 done

Last edited by Nicole2320; 03-19-2021 at 04:38 PM.
 
Old 03-19-2021, 02:29 PM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
1. please use code tags to post your script
2. for me it [the functionality] looks very similar to bullshit generators, so you may try to check them (I mean the code of those generators), probably you will find something suitable.
 
  


Reply



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
grep, egrep, fgrep two outputs to two different files [Bash] aurquiel Programming 5 11-11-2017 11:58 AM
how to convert unformatted binary files/outputs to netcdf outputs in linux? sarasari Programming 2 03-11-2014 11:30 PM
How do i create a calendar/remind program that outputs "xmessage" or similar?? kline Programming 3 08-11-2010 05:20 PM
One Part Horror Story; One Part Success Story davidstvz General 3 08-25-2009 04:02 PM
Mplayer and Xine create only green outputs on playing video files sn_piranha Linux - Software 5 10-10-2004 08:36 PM

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

All times are GMT -5. The time now is 09:33 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
Open Source Consulting | Domain Registration