0

Is there a way to grab a 'random matching' string via bash from a text file?

I am currently grabbing a download link via bash, curl & grep from a online text file.

Example:

DOWNLOADSTRING="$(curl -o - "http://example.com/folder/downloadlinks.txt" | grep "$VARIABLE")"

from online text file which contains

http://alphaserver.com/files/apple.zip
http://alphaserver.com/files/banana.zip

where $VARIABLE is something the user selected.

Works great, but i wanted to add some mirrors to the text file.

So when the variable 'banana' is selected, text file which i grep contains:

http://alphaserver.com/files/apple.zip
http://betaserver.com/files/apple.zip
http://gammaserver.com/files/apple.zip
http://deltaserver.com/files/apple.zip
http://alphaserver.com/files/banana.zip
http://betaserver.com/files/banana.zip
http://gammaserver.com/files/banana.zip
http://deltaserver.com/files/banana.zip

the code should pick a random 'banana' string and store it as the 'DOWNLOADSTRING' variable. the current code above can only work with 1 string in the text file, since it grabs everything 'banana'.

What this is for; i wanted to add some mirror downloadlinks for the files in the online text file, and the current code doesn't allow that.

Can i let grep grab one random 'banana' string? (and not all of them)

devilhunter
  • 321
  • 1
  • 4
  • 13
  • Would it be enough to pick the first matched "string"? That can be done with `grep -m 1 banana` . Flag -m specifies the maximum number of found lines – bbaja42 Jun 16 '11 at 17:18

3 Answers3

3

See this question to see how to get a random line after grep. rl seems like a good candidate

What's an easy way to read random line from a file in Unix command line?

then do a grep ... | rl | head -n 1

Community
  • 1
  • 1
dfb
  • 13,133
  • 2
  • 31
  • 52
2

Try this:

DOWNLOADSTRING="$(curl -o - "http://example.com/folder/downloadlinks.txt" | grep "$VARIABLE")" |
    sort -R | head -1

The output will be random-sorted and then the first line will be selected.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Note that "|" is a command separator (like ";") so you don't need the line continuation character "\" – glenn jackman Jun 16 '11 at 18:41
  • I think I once knew that ! I'm saying that because I was not sure whether I need a backslash or not; but I put it anyway, to make sure. Thanks for pointing that out, I'll edit the answer. – Costi Ciudatu Jun 16 '11 at 22:20
0

If mirrors.txt has the following data, which you provided in your question:

http://alphaserver.com/files/apple.zip
http://betaserver.com/files/apple.zip
http://gammaserver.com/files/apple.zip
http://deltaserver.com/files/apple.zip
http://alphaserver.com/files/banana.zip
http://betaserver.com/files/banana.zip
http://gammaserver.com/files/banana.zip
http://deltaserver.com/files/banana.zip

Then you can use the following command to get a random "matched string" from the file:

grep -E "${VARIABLE}" mirrors.txt | shuf -n1

Then you can store it as the variable DOWNLOADSTRING by setting it's value with a function call like so:

rand_mirror_call() { grep -E "${1}" mirrors.txt | shuf -n1; }
DOWNLOADSTRING="$(rand_mirror_call ${VARIABLE})"

This will give you a dedicated random line from the text file based on the user's ${VARIABLE} input. It is a lot less typing this way.

Yokai
  • 1,170
  • 13
  • 17