Bash Script to Make a Random Selection of Lines in a File by droida

View this thread on steempeak.com
· @droida · (edited)
$6.14
Bash Script to Make a Random Selection of Lines in a File
# randomlines.sh [--verbose] \<FILE> \<COUNT>
Prints COUNT lines from FILE randomly

## Usecases
You have a list of cities you wish to travel to during your summer holidays but you don't know which ones to visit first. This program for Linux, ***randomlines.sh*** can help you to decide where to go first. It also could be used to randomly elect steemians or posts for a contest.

## Format the Data
Put the list in a file. One item, one line.

***towns.csv***
```
Amsterdam
Athens
...
Stockholm
Vienna
```

## The command
To know the 5 next cities to travel to, just type the command in a terminal:
```bash
$ ./randomlines.sh towns.csv 5
```

It produces the output:
```
Paris
Vienna
Geneva
Monaco
Athens
```

### Verbose Option
Unfortunately, it doesn't post the result in the blockchain yet ;)
```bash
$ ./randomlines.sh --verbose towns.csv 5
towns.csv has 16 lines
Drawing 5 numbers between 1 and 16
Numbers:  12 3 6 10 16
#12 Paris
#3 Barcelona
#6 Brussels
#10 Monaco
#16 Vienna
```

https://www.macworld.co.uk/cmsdata/features/3608274/Terminalicon2_thumb800.png

# The source code
I'm lazy, nevertheless I added comments.
```bash
#!/bin/bash

# function to print a random number between $1 and $2
randint() {
    min=$1
    max=$2
    # modulo
    let "mod=1+max-min"
    # big random hex number (16 hex numbers, uppercase)
    h=$(openssl rand -hex 16 | tr [a-z] [A-Z])
    # decimal conversion
    n=$(echo "ibase=16; $h" | bc)
    # print the random number
    echo "($n % $mod) + $min" | bc
}

# parameters
if [[ "$1" == "--verbose" ]] ; then
    verbose=0 # true
    shift
fi

file=$1
todraw=$2

# checks
if [[ "$todraw" == "" ]] ; then
    echo "Usage $0 [--verbose] <FILE> <COUNT>";
    exit 1
fi
if [ ! -e "$file" ] ; then
    echo "File $file does not exist"
    exit 1
fi

# limits
min=1
max=$(wc -l $file | cut -d\  -f1)
if [ $verbose ] ; then echo "$file has $max lines" ; fi
if [[ "$todraw" -gt "$max" ]] ; then
    echo "File $file has too few lines ($max) to draw $todraw distinct items"
    exit 1
fi

# draw distinct random numbers
if [ $verbose ] ; then echo "Drawing $todraw numbers between $min and $max" ; fi
count=0
numbers=""
while [[ "$count" != "$todraw" ]] ; do
    value=$(randint $min $max)
    found=$(echo $numbers | egrep "^$value | $value | $value$")
    
    if [[ "$found" == "" ]] ; then
        numbers="$numbers $value"
        let "count=count+1"
    fi
done
if [ $verbose ] ; then echo "Numbers: $numbers" ; fi

# print elected lines
for number in $numbers ; do
    if [ $verbose ] ; then echo -n "#$number " ; fi
    # print the corresponding line of the file
    sed -n "$number"p $file
done

exit 0
```

I hope you'll find it useful. Questions, suggestions and remarks are welcome.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 13 others
👎  , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id81,389,769
authordroida
permlinkbash-script-to-make-a-random-selection-of-lines-in-a-file
categorybash
json_metadata{"app":"steempeak\/1.19.0","format":"markdown","tags":["bash","linux","development","neoxian","programming","dev","cli","terminal","random","technology"],"image":["https:\/\/www.macworld.co.uk\/cmsdata\/features\/3608274\/Terminalicon2_thumb800.png"]}
created2019-11-02 00:38:54
last_update2019-11-02 00:50:12
depth0
children8
net_rshares21,448,092,902,886
last_payout2019-11-09 00:38:54
cashout_time1969-12-31 23:59:59
total_payout_value3.076 SBD
curator_payout_value3.067 SBD
pending_payout_value0.000 SBD
promoted0.120 SBD
body_length2,729
author_reputation6,341,941,362,400
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (98)
@hoaithu ·
**Hello @droida**

Thank you for following @haccolong account. As a follower of **@haccolong** this post has been randomly selected and **[upvoted](https://steemit.com/free/@haccolong/get-free-upvote-daily-when-follow-me)** by @hoaithu's Curation Trail.

To **earn more** rewards with your Steemit account. Check through some of the ways at **[this post]( https://steemit.com/free/@haccolong/how-to-get-more-rewards-if-earning-rewards-is-difficult-for-you-this-post-is-for-you)**.
##### I will continue with random upvotes in the future & wish you lots of luck. :)
properties (22)
post_id81,391,155
authorhoaithu
permlinkq0blec
categorybash
json_metadata{"users":["droida","haccolong","hoaithu"],"links":["https:\/\/steemit.com\/free\/@haccolong\/get-free-upvote-daily-when-follow-me","https:\/\/steemit.com\/free\/@haccolong\/how-to-get-more-rewards-if-earning-rewards-is-difficult-for-you-this-post-is-for-you"],"app":"steemit\/0.1"}
created2019-11-02 02:25:27
last_update2019-11-02 02:25:27
depth1
children0
net_rshares0
last_payout2019-11-09 02:25:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length564
author_reputation4,342,879,484,296
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars0
@curationkiwi ·
title
You got voted by @curationkiwi thanks to droida! This bot is managed by [KiwiJuce3](https://bit.ly/2HQOvIO) and run by [Rishi556](https://bit.ly/2OmBsQF), you can check both of them out there. To receive upvotes on your own posts, you need to join the [Kiwi Co. Discord](https://bit.ly/2U8Mo9M) and go to the room named #CurationKiwi. Submit your post there using the command  "!upvote (post link)" to receive upvotes on your post. CurationKiwi is currently supported by donations from users like you, so feel free to leave an upvote on our posts or comments to support us!
We have also recently added a new whitelist feature for those who would like to support CurationKiwi even more! If you would like to receive upvotes more than 2x greater than the normal upvote, all you need to do is delegate 50 SP to @CurationKiwi using [this link](https://app.steemconnect.com/sign/delegateVestingShares?account=curationkiwi&delegatee=curationkiwi&vesting_shares=99572.000000%20VESTS).
properties (22)
post_id81,402,516
authorcurationkiwi
permlinkre-bash-script-to-make-a-random-selection-of-lines-in-a-file
categorybash
json_metadata{"app":"Discord"}
created2019-11-02 13:56:03
last_update2019-11-02 13:56:03
depth1
children0
net_rshares0
last_payout2019-11-09 13:56:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length977
author_reputation2,747,191,214,125
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@drotto ·
<p>This post has received a 3.13 % upvote from @drotto thanks to: @curationkiwi.</p>
properties (22)
post_id81,402,558
authordrotto
permlinkre-droida-bash-script-to-make-a-random-selection-of-lines-in-a-file-20191102t135804306z
categorybash
json_metadata{"tags":["bash"],"app":"drotto\/0.0.5pre2"}
created2019-11-02 13:58:03
last_update2019-11-02 13:58:03
depth1
children0
net_rshares0
last_payout2019-11-09 13:58:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length85
author_reputation424,402,347,817
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@boomerang ·
This post has received a 100.0 % upvote from @boomerang.
properties (22)
post_id81,413,680
authorboomerang
permlinkre-bash-script-to-make-a-random-selection-of-lines-in-a-file-20191102t234748
categorybash
json_metadata{}
created2019-11-02 23:47:48
last_update2019-11-02 23:47:48
depth1
children0
net_rshares0
last_payout2019-11-09 23:47:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length56
author_reputation3,467,368,504,525
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@minnowvotes ·
re-droida-bash-script-to-make-a-random-selection-of-lines-in-a-file-20191103t055847201z
You got a 10.00% upvote from @minnowvotes courtesy of @droida!
properties (22)
post_id81,419,061
authorminnowvotes
permlinkre-droida-bash-script-to-make-a-random-selection-of-lines-in-a-file-20191103t055847201z
categorybash
json_metadata{"app":"postpromoter\/2.1.1"}
created2019-11-03 05:58:48
last_update2019-11-03 05:58:48
depth1
children0
net_rshares0
last_payout2019-11-10 05:58:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length63
author_reputation-300,453,853,020
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@theguruasia ·
!trendovoter 100
👍  
properties (23)
post_id81,482,166
authortheguruasia
permlinkre-droida-q0hx7h
categorybash
json_metadata{"tags":["bash"],"app":"steempeak\/1.19.0"}
created2019-11-05 12:26:06
last_update2019-11-05 12:26:06
depth1
children1
net_rshares9,266,534,663
last_payout2019-11-12 12:26:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length16
author_reputation59,186,440,518,630
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@droida ·
Thanks
properties (22)
post_id81,489,723
authordroida
permlinkre-theguruasia-q0ibgy
categorybash
json_metadata{"tags":["bash"],"app":"steempeak\/1.19.0"}
created2019-11-05 17:34:09
last_update2019-11-05 17:34:09
depth2
children0
net_rshares0
last_payout2019-11-12 17:34:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length6
author_reputation6,341,941,362,400
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@trendovoter ·
Congratulations @theguruasia, <b>100%</b> upvote has been shared with your successful call on the post that shared by @droida!

---
<sup>Support <b>@trendotoken</b> projects by delegating : <b>[100SP](https://beta.steemconnect.com/sign/delegateVestingShares?delegator=YOURUSERNAME&delegatee=trendovoter&vesting_shares=100.000%20SP)</b> , <b>[200SP](https://beta.steemconnect.com/sign/delegateVestingShares?delegator=YOURUSERNAME&delegatee=trendovoter&vesting_shares=200.000%20SP) , <b>[500SP](https://beta.steemconnect.com/sign/delegateVestingShares?delegator=YOURUSERNAME&delegatee=trendovoter&vesting_shares=500.000%20SP)</b> , <b>[1000SP](https://beta.steemconnect.com/sign/delegateVestingShares?delegator=YOURUSERNAME&delegatee=trendovoter&vesting_shares=1000.000%20SP)</b> , <b>[2000SP](https://beta.steemconnect.com/sign/delegateVestingShares?delegator=YOURUSERNAME&delegatee=trendovoter&vesting_shares=2000.000%20SP)</b></sup>
properties (22)
post_id81,482,175
authortrendovoter
permlinkre-droida-bash-script-to-make-a-random-selection-of-lines-in-a-file-20191105t122620881z
categorybash
json_metadata{"tags":["trdo","newssteem"],"app":"trdo-voter\/1.0","format":"markdown"}
created2019-11-05 12:26:21
last_update2019-11-05 12:26:21
depth1
children0
net_rshares0
last_payout2019-11-12 12:26:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length938
author_reputation60,565,069,820
root_title"Bash Script to Make a Random Selection of Lines in a File"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000