gpt4 book ai didi

linux - 尝试使用随机结尾的 grep url

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:22 26 4
gpt4 key购买 nike

网址始终以 8 个随机字符结尾。
我可以很容易地 grep https://websitef.com/

grep https://websitef.com/测试.txt

但无法弄清楚如何获得后面的那 8 个随机字符

这是它在文件中的样子:

..."num_comments": 16, "url": "https://websitef.com/vkl6owav", "_has_fetched": true.....    

最佳答案

如果您的输入是 JSON,您可能需要考虑使用特定于 JSON 的工具。

让我们考虑一下您的测试文件:

$ cat file
..."num_comments": 16, "url": "https://websitef.com/vkl6owav", "_has_fetched": true.....

grep你想要的字符串:

$ grep -Po '(?<=https://websitef.com/)\w+' file
vkl6owav

\w+匹配一串单词字符。 (?<=https://websitef.com/)是一个 look-behind,它将匹配限制在字符串 https://websitef.com/ 之后的字符.这需要 GNU grep。

如果 GNU grep 不可用,sed可以使用:

$ sed -En 's|.*https://websitef.com/([[:alnum:]]+).*|\1|p' file
vkl6owav

如果您想要整个 URL,而不仅仅是随机字符串:

$ grep -o 'https://websitef.com/[[:alnum:]]*' file
https://websitef.com/vkl6owav

关于linux - 尝试使用随机结尾的 grep url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39578103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com