我有一个配置文件,其内容如下:
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命令,但无法正确使用它。
答案 0 :(得分:6)
使用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
答案 1 :(得分: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
答案 2 :(得分: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