ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
#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.
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.
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?
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.
#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
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.
#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
#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"
#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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.