2013-08-20 20 views
0

提取IP地址具有其內容如下的配置文件:我如何從一個url

db.path=/home/kpmg/test/dbfiles/dbone.db 
db.connection.timeout=500000 
server.url=http://a.b.c.d:8033/plain/httpds? 
output.file.path=/home/kpmg/test/dbfiles/ 
log.file.path=/home/kpmg/test/dbfiles/ 
log.level=DEBUG 

我需要從server.url線只提取IP地址。我通過閱讀示例和文檔嘗試使用了幾個awk命令,但無法做到正確。與awk

回答

5

一種方式來打印只是IP:

$ awk -F'[/:]' '/server[.]url/{print $4}' file 
a.b.c.d 

或者使用GNU grep

$ grep -Po '(?<=server.url=http://)[^:]*' file 
a.b.c.d 

或爲IP的只是grep

$ egrep -o '([0-9]{1,3}[.]){3}[0-9]{1,3}' file 
+1

感謝這個!有用!再次感謝! – Anirudh

1
awk -F'http://|:[0-9]+' '$0=$2' file 

這適用於你的例子:

kent$ echo "db.path=/home/kpmg/test/dbfiles/dbone.db 
db.connection.timeout=500000 
server.url=http://a.b.c.d:8033/plain/httpds? 
output.file.path=/home/kpmg/test/dbfiles/ 
log.file.path=/home/kpmg/test/dbfiles/ 
log.level=DEBUG"|awk -F'http://|:[0-9]+' '$0=$2' 
a.b.c.d 
0
echo "${URL}" | awk -F/ '{print $3}' | sed 's/:.*//' 

這裏是一個測試對各種可能性的要點:https://gist.github.com/leodotcloud/c4cbedaabeba0a06956188a04022e9ce

採樣運行:

# ./get_domain_from_url.sh 
For URL:http://1.1.1.1, domain name:1.1.1.1 
For URL:http://1.1.1.1:8443, domain name:1.1.1.1 
For URL:https://1.1.1.1, domain name:1.1.1.1 
For URL:https://1.1.1.1:8443, domain name:1.1.1.1 
For URL:http://hello-world, domain name:hello-world 
For URL:http://hello-world.com, domain name:hello-world.com 
For URL:http://sub.hello-world.com, domain name:sub.hello-world.com 
For URL:http://hello-world:8080, domain name:hello-world 
For URL:http://hello-world.com:8080, domain name:hello-world.com 
For URL:http://sub.hello-world.com:8080, domain name:sub.hello-world.com 
For URL:https://hello-word/foo/bar, domain name:hello-word