如何在Bash中从URL中提取域名?
例如: http://example.com/ 转成 example.com
必须适用于任何顶级域名,而不仅仅是 .com。
echo http://example.com/index.php | awk -F[/:] '{print $4}'
输出:example.com
:-)
echo http://example.com:3030/index.php | awk -F/ '{print $3}'
翻译成中文后是:example.com:3030
。 - Ben Burns:
分割它,但它不够灵活,无法接受带或不带端口的情况。 - chovyhttp://www.example.com/somedir/someotherdir/index.html
| cut -d'/' -f1,2,3,得到的结果是 http://www.example.com
。 - 3AKawk -F[/:] '{print $4}'
- Michael$ URI="http://user:pw@example.com:80/"
$ echo $URI | sed -e 's/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/'
example.com
echo http://www.mail.example.com:3030/index.php | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/" | awk -F. '{print $(NF-1) "." $NF}'
,因此基本上我通过点将你的输出划分,并取最后两列并用点连接它们。 - sakumattosed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\)\(:\([0-9]\{1,5\}\)\)\?.*/\4/"
- wheelerbasename "http://example.com"
当然,这种URI http://www.example.com/index.html
是行不通的,但是你可以采取以下方法:
basename $(dirname "http://www.example.com/index.html")
或者对于更复杂的URI:
echo "http://www.example.com/somedir/someotherdir/index.html" | cut -d'/' -f3
-d 意味着 "分隔符",而 -f 意味着 "字段";在上面的示例中,由斜杠 '/' 分隔的第三个字段是 www.example.com。
echo "http://www.example.com:8080/somedir/someotherdir/index.html" | cut -d'/' -f3
。 - chovyhttp://www.example.com/somedir/someotherdir/index.html | cut -d'/' -f1,2,3
,获取了此链接 - http://www.example.com
。 - 3AKbasename $(dirname
does not work, if the url ends with the domain like: basename $(dirname "http://www.example.com/")
will show just: http:
- rubo77echo $URL | cut -d'/' -f3 | cut -d':' -f1
适用于以下URL:
http://host.example.com
http://host.example.com/hi/there
http://host.example.com:2345/hi/there
http://host.example.com:2345
abc.com
将保留为abc.com
。 - Udayraj Deshmukhhost.example.com
而不是所要求的域名(example.com
)。 - Lucassed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_'
例如
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://example.com'
example.com
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'https://example.com'
example.com
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://example.com:1234/some/path'
example.com
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path'
example.com
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path#fragment'
example.com
$ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path#fragment?params=true'
example.com
HOST=$(sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< "$MYURL")
在 Bash 中是可以的。 - 4Z4T4Rsed -E -e 's_.*://([^/@]*@)?([^/:]+)(.*)_\2_' <<< 'http://example.com'
这将允许您从URL中抓取路径
sed -E -e 's_.://([^/@]@)?([^/:]+)(.*)\3' <<< 'http://example.com/path/to/something' - Max Barrass#!/usr/bin/perl -w
use strict;
my $url = $ARGV[0];
if($url =~ /([^:]*:\/\/)?([^\/]+\.[^\/]+)/g) {
print $2;
}
使用方法:
./test.pl 'https://example.com'
example.com
./test.pl 'https://www.example.com/'
www.example.com
./test.pl 'example.org/'
example.org
./test.pl 'example.org'
example.org
./test.pl 'example' -> no output
如果你只需要域名而不是完整的主机名+域名,可以使用以下方法:
#!/usr/bin/perl -w
use strict;
my $url = $ARGV[0];
if($url =~ /([^:]*:\/\/)?([^\/]*\.)*([^\/\.]+\.[^\/]+)/g) {
print $3;
}
/
字符上“分割”字符串!使用正则表达式来完成这种任务是杀鸡用牛刀!URL="http://example.com/some/path/to/page.html"
prot="${URL%%:*}"
link="${URL#$prot://}"
domain="${link%%/*}"
link="${link#$domain}"
printf '%-8s: %s\n' Protocol "${prot%:}" Domain "$domain" Link "$link"
Protocol: http
Domain : example.com
Link : /some/path/to/page.html
注意:这项工作即使使用 file
URL也可以完成:
URL=file:///tmp/so/test.xml
prot="${URL%%:*}"
link="${URL#$prot://}"
domain="${link%%/*}"
link="${link#$domain}"
printf '%-8s: %s\n' Protocol "${prot%:}" Domain "$domain" Link "$link"
Protocol: file
Domain :
Link : /tmp/so/test.xml
read
的简短、快速和可靠的解决方案:URL="http://example.com/some/path/to/page.html"
IFS=/ read -r prot _ domain link <<<"$URL"
printf '%-8s: %s\n' Protocol "${prot%:}" Domain "$domain" Link "/$link"
Protocol: http
Domain : example.com
Link : /some/path/to/page.html
URL="http://example.com:8000/some/path/to/page.html"
IFS=/ read -r prot _ domain link <<<"$URL"
IFS=: read -r domain port <<<"$domain"
printf '%-8s: %s\n' Protocol "${prot%:}" Domain "$domain" Port "$port" Link "/$link"
Protocol: http
Domain : example.com
Port : 8000
Link : /some/path/to/page.html
URL="https://dev59.com/h3E95IYBdhLWcg3wApLl"
declare -A DEFPORTS='([http]=80 [https]=443 [ipp]=631 [ftp]=21)'
IFS=/ read -r prot _ domain link <<<"$URL"
IFS=: read -r domain port <<<"$domain"
printf '%-8s: %s\n' Protocol "${prot%:}" Domain "$domain" \
Port "${port:-${DEFPORTS[${prot%:}]}}" Link "/$link"
Protocol: https
Domain : stackoverflow.com
Port : 443
Link : /questions/2497215/how-to-extract-domain-name-from-url
关于公共后缀和@tripleee'的评论
在函数初始化时,有一个对wget
的分支只执行一次:
declare -A TLD='()'
initTld () {
local tld
while read -r tld; do
[[ -n ${tld//*[ \/;*]*} ]] && TLD["${tld#\!}"]=''
done < <(
wget -qO - https://publicsuffix.org/list/public_suffix_list.dat
)
}
tldExtract () {
if [[ $1 == -v ]] ;then local _tld_out_var=$2;shift 2;fi
local dom tld=$1 _tld_out_var
while [[ ! -v TLD[${tld}] ]] && [[ -n $tld ]]; do
IFS=. read -r dom tld <<< "$tld"
done
if [[ -v _tld_out_var ]] ;then
printf -v $_tld_out_var '%s %s' "$dom" "$tld"
else
echo "$dom $tld"
fi
}
initTld ; unset -f initTld
那么
tldExtract www.stackoverflow.com
stackoverflow com
tldExtract sub.www.test.co.uk
test co.uk
tldExtract -v myVar sub.www.test.co.uk
echo ${myVar% *}
test
echo ${myVar#* }
co.uk
tldExtract -v myVar www2.sub.city.nagoya.jp
echo $myVar
sub city.nagoya.jp
parseUrl() { local IFS=/ arry;arry=($4);printf -v $1 ${arry%:};printf -v $2 ${arry[2]};printf -v $3 "/${arry[*]:3}";}
可用作 read
的替代品:parseUrl prot domain link "$URL"
以填充 $prot $domain
和 $link
变量。 - F. Hauri - Give Up GitHub不必使用正则表达式,您可以使用Python的urlparse:
URL=http://www.example.com
python -c "from urlparse import urlparse
url = urlparse('$URL')
print url.netloc"
您可以按照以下方式使用它,或将其放入一个小脚本中。然而,这仍然需要一个有效的方案标识符,根据您的评论,您的输入不一定提供一个。您可以指定一个默认方案,但urlparse期望netloc以'//'
开头:
url = urlparse('//www.example.com/index.html','http')
因此,您需要手动添加这些内容,即:
python -c "from urlparse import urlparse
if '$URL'.find('://') == -1 then:
url = urlparse('//$URL','http')
else:
url = urlparse('$URL')
print url.netloc"
关于如何获取这些URL的信息非常少,请在下次提供更多信息。URL中是否有参数等等...
与此同时,对于您的示例URL,只需进行简单的字符串操作即可。
$ s="http://example.com/index.php"
$ echo ${s/%/*} #get rid of last "/" onwards
http://example.com
$ s=${s/%\//}
$ echo ${s/#http:\/\//} # get rid of http://
example.com
另外一种方法是使用sed(GNU)
$ echo $s | sed 's/http:\/\///;s|\/.*||'
example.com
使用awk
$ echo $s| awk '{gsub("http://|/.*","")}1'
example.com
URI="http://user@example.com/foo/bar/baz/?lala=foo"
ruby -ruri -e "p URI.parse('$URI').host"
如果您想了解如何使用Ruby的URI类,可以参考文档。
www.surrey.bbc.co.uk
、www.nic.ad.jp
、www.city.nagoya.jp
等情况。 - tripleee