Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I created a POSIX shell script alternative to randomize.py #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mutageneral
Copy link

@mutageneral mutageneral commented Sep 1, 2024

I created a POSIX shell script alternative to randomize.py that should work on all Linux, BSD and macOS systems out of the box, no Python required.
I think this fits well with the "only use the standard c libraries" goal of the project by providing a portable alternative to the Python script.


Usage example:

$ head most_used_words.txt
determine said had idea for do might touch safe village step travel phrase hunt
song free wish mountain problem has buy son once score dollar protect tree two
during force out piece dry told leg thank arm ready body south picture stretch
heart me school equate similar such join time seem your flat copy guess get
last bell wash kind door distant slow subject branch exact clean search name
off atom dream imagine hear fit group heavy wave a develop syllable natural
wood plural tube raise got thick must him break her can liquid answer possible
am basic bar make weather before corn stood read letter garden at much shore
large drink gas move rose window pretty test skin sugar fall star saw press
pull insect either miss tell type print nor consider wing heard follow toward

$ ./randomize.sh most_used_words.txt

$ head most_used_words.txt
sleep here triangle able warm than see slow if substance road fresh ride let
job language carry run make material question too suit only planet what iron
climb truck equate name fly found track represent force hot same mother pretty
pay art listen hold read claim had year long difficult necessary could area one
consonant fight small original dollar water melody again set land ice first
matter study can raise operate touch she supply why spring floor win heat count
collect cry best save leg start surprise desert as protect broad clock prove
teeth such suggest cat dad early fraction usual number tell term done you heavy
an consider trouble joy car chart magnet check pull give box glad bring those
crop mile card toward bat person decimal do example hole particular bank nature

Oh, and another thing. I can't help with this until I learn C, but on POSIX systems we have pipes, so I would like if typist could read the words from stdin so you could do something like this:
$ randomize.sh most_used_words.txt | typist -
(I would modify randomize.sh to output out of stdout by default.)

This is better because we don't have to write to the disk.
Another benefit: randomize.sh wouldn't overwrite the file, so you could pass it paths of text files without having to make a copy first.

It also gives us more extensibility and power.
For example, if you want a shorter test:
$ randomize.sh most_used_words.txt | head -1 | typist -
It keeps only the first line of the randomized text.

Or you could type a fortune cookie/quote:
$ fortune | typist -

Or a manual page:
$ man 1p sed | typist -

POSIX shell script alternative to randomize.py that should work on all
Linux, BSD and MacOS systems out of the box
# clean up each word: remove leading and trailing punctuation and quotes
val=$(printf "%s" "$val" | sed -e 's/^[({'"'"'"]*//' -e 's/[.,!?;:)]}'"'"'"]*$//')
words="$words
$val" # one word per line in $words var
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better?

words="${words}${words:+$'\n'}$val"

It seems to work the same way and fits on a single line, but it's less understandable.

set -- $line
for val; do
# clean up each word: remove leading and trailing punctuation and quotes
val=$(printf "%s" "$val" | sed -e 's/^[({'"'"'"]*//' -e 's/[.,!?;:)]}'"'"'"]*$//')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would do the same thing as this cryptic sed command and better, but I will be honest, I haven't tried the punctuation cleaning feature with eider commands.

# delete leading and trailing punctuation and whitespace from each line
sed -e 's/^[[:punct:][:space:]]*//' -e 's/[[:punct:][:space:]]*$//'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant