bash grep ‘随机匹配’细绳
有没有办法通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅此问题以了解如何在 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 candidateWhat's an easy way to read random line from a file in Unix command line?
then do a
grep ... | rl | head -n 1
试试这个:
输出将被随机排序,然后第一行将被选择。
Try this:
The output will be random-sorted and then the first line will be selected.
如果
mirrors.txt
具有您在问题中提供的以下数据:那么您可以使用以下命令从文件中获取随机“匹配的字符串”:
然后您可以将其存储为变量
DOWNLOADSTRING
,方法是使用函数调用设置其值,如下所示:这将根据用户的
${VARIABLE}< 从文本文件中为您提供专用的随机行/代码> 输入。这样打字就少了很多。
If
mirrors.txt
has the following data, which you provided in your question:Then you can use the following command to get a random "matched string" from the file:
Then you can store it as the variable
DOWNLOADSTRING
by setting it's value with a function call like so: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.