Bash 使用sed或grep拆分url
例如,我有这样一个url-->Bash 使用sed或grep拆分url,bash,sed,grep,Bash,Sed,Grep,例如,我有这样一个url-->http://google.com/test/to_be_extracted.html我想拆分此url并仅提取部分。我想排除http://google.com/test/和.html部分 如何使用GREP或SED设置regex模式 为什么您需要解决方案来包含不必要的工具 basename "$url" .html 将以琐碎和透明的方式满足您的要求。您可以使用以下方法: $ echo 'http://google.com/test/to_be_extracted.h
http://google.com/test/to_be_extracted.html
我想拆分此url并仅提取部分。我想排除http://google.com/test/
和.html
部分
如何使用GREP或SED设置regex模式 为什么您需要解决方案来包含不必要的工具
basename "$url" .html
将以琐碎和透明的方式满足您的要求。您可以使用以下方法:
$ echo 'http://google.com/test/to_be_extracted.html' | sed -r 's#.*\/([^.]+).*#\1#'
to_be_extracted
细分:
事实上,我试图归档我的目标就像,echo'http://google.com/test/to_be_extracted.html“| sed”s/*www.google.com/test/”
但此代码仅返回此/to_be_dextract.html
我不想要/和.html
。是的,它正在工作!“谢谢,你能解释一下你的正则表达式模式吗?我对正则表达式很陌生。”易卜拉欣补充道。希望对您有所帮助。非常感谢@jaypalWelcome to Stack Overflow!看起来你想让我们为你写些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是,包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、堆栈跟踪、编译器错误——任何适用的)。你提供的细节越多,你可能得到的答案就越多。检查和
sed -r ' # -r switch enables Extended Regular expressions
s # Using substitution flag
# # Using # as delimiter since you have `/` in your lines
.*\/ # Match everything greedily until you see last `/`.
([^.]+) # Create a capture group to capture everything until you see a literal .
.* # Followed by everything else
# # Another delimiter
\1 # Print the captured group
#' # Final delimiter