使用sed或grep分割网址

时间:2014-03-27 17:48:51

标签: bash sed grep

例如,我有一个这样的网址 - > http://google.com/test/to_be_extracted.html我想拆分此网址并仅获取to_be_extracted部分。我想要排除http://google.com/test/.html部分。

如何使用GREP或SED设置正则表达式模式?

2 个答案:

答案 0 :(得分:2)

您可以使用:

$ echo 'http://google.com/test/to_be_extracted.html' | sed -r 's#.*\/([^.]+).*#\1#'
to_be_extracted

故障:

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

答案 1 :(得分:0)

为什么您需要解决方案来使用不必要的工具?

basename "$url" .html

会做你需要的,平凡而透明的。