尝试使用随机结尾

时间:2016-09-19 16:43:21

标签: linux url grep

网址总是以8个随机字符结尾 我可以轻松地使用

grep https://websitef.com/

grep https://websitef.com/ test.txt

但是无法弄清楚如何获得

之后的8个随机字符

这就是文件中的样子:

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

1 个答案:

答案 0 :(得分:0)

如果您的输入是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/)是一个后视,将匹配限制在字符串https://websitef.com/后面的字符。这需要GNU grep。

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

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

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

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