1

我有一个配置文件,其内容如下:

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 命令,但无法正确使用。

4

3 回答 3

7

awk仅打印 IP 的一种方法:

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

或使用GNU grep

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

或仅grep针对 IP:

$ egrep -o '([0-9]{1,3}[.]){3}[0-9]{1,3}' file
于 2013-08-20T12:01:31.537 回答
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
于 2013-08-20T12:15:37.213 回答
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
于 2018-03-09T18:58:57.190 回答