bash grep ‘随机匹配’细绳

发布于 2024-11-16 04:41:25 字数 987 浏览 1 评论 0原文

有没有办法通过 bash 从文本文件中获取“随机匹配”字符串?

我目前正在通过 bash、curl & 获取下载链接。 grep 来自在线文本文件。

示例:

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

来自在线文本文件,其中

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

$VARIABLE 是用户选择的内容。

效果很好,但我想在文本文件中添加一些镜像。

因此,当选择变量“banana”时,我 grep 包含的文本文件:

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

代码应选择随机“banana”字符串并将其存储为“DOWNLOADSTRING”变量。 上面的当前代码只能处理文本文件中的 1 个字符串,因为它捕获所有“banana”。

这是做什么用的;我想为在线文本文件中的文件添加一些镜像下载链接,但当前代码不允许这样做。

我可以让 grep 抓取一个随机的“banana”字符串吗? (而且不是全部)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守护在此方 2024-11-23 04:41:25

请参阅此问题以了解如何在 grep 之后获取随机行。 rl 似乎是一个不错的候选人

在 Unix 命令行中从文件中读取随机行的简单方法是什么?

然后执行 grep ... | RL |头 -n 1

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

恬淡成诗 2024-11-23 04:41:25

试试这个:

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

输出将被随机排序,然后第一行将被选择。

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.

锦欢 2024-11-23 04:41:25

如果 mirrors.txt 具有您在问题中提供的以下数据:

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

那么您可以使用以下命令从文件中获取随机“匹配的字符串”

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

然后您可以将其存储为变量 DOWNLOADSTRING,方法是使用函数调用设置其值,如下所示:

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

这将根据用户的 ${VARIABLE}< 从文本文件中为您提供专用的随机行/代码> 输入。这样打字就少了很多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文