bash grep“随机匹配"字符串 [英] bash grep 'random matching' string

查看:83
本文介绍了bash grep“随机匹配"字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过bash从文本文件中获取随机匹配"字符串?

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

我目前正在通过bash,curl& amp;抓取下载链接.在线文本文件中的grep.

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

示例:

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

其中$ VARIABLE是用户选择的内容.

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:

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

代码应选择一个随机的香蕉"字符串并将其存储为"DOWNLOADSTRING"变量.上面的当前代码只能在文本文件中使用1个字符串,因为它捕获了所有香蕉".

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.

我可以让grep抓取一个随机的香蕉"字符串吗?(并非全部)

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

推荐答案

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

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

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

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

这篇关于bash grep“随机匹配"字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆