Bash:将绝对URL转换为相对URL

时间:2015-11-03 15:04:07

标签: regex bash awk grep

我有一个绝对网址列表,想要过滤前面的部分。例如。 http://www.domain.tld/example转到/ example

此部分保存在变量domain=www.domain.tld中。没有http,显而易见。

经过多次尝试后我的代码是(由于正则表达式而导致的转义点):grep -o -v "http://${domain//./\\.}"

它不能很好地工作......任何有解决方案的人?也许光滑的awk?

2 个答案:

答案 0 :(得分:1)

假设所有网址都包含协议,您可以使用cut /作为分隔符,并从第4个字段打印:

cut -d'/' -f4- file

测试

$  cat a
http://www.domain.tld/example 
http://www.another.doma.in/and/therest
$ cut -d'/' -f4- a
example 
and/therest

答案 1 :(得分:0)

您应该使用:

domain='domain.tld'

允许在网址中同时使用www.domain.tlddomain.tld

然后使用此gnu-grep命令:

grep -oP "https?://(www\.)?$domain\K/\S*" file
/example
  • https? - 将同时匹配httphttps
  • (www\.)? - 会在www.变量
  • 之前选择$domain
  • \K - 用于PCRE正则表达式中的匹配重置,以丢弃此时的匹配信息
  • -o - 仅输出匹配的文字
  • -P - 用于PCRE
  • 中的grep正则表达式