[求助]如何提取文件中出现的ip:port[已解决]

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
bigsun
帖子: 301
注册时间: 2009-01-11 16:05

[求助]如何提取文件中出现的ip:port[已解决]

#1

帖子 bigsun » 2012-06-27 20:20

示例文件:

代码: 全选

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body><p>Here are your bridge relays: <pre id="bridges">bridge 83.226.165.182:443 
bridge 78.147.155.162:9001 
bridge 86.204.237.150:443 
</pre></p><ul><li>gmail.com</li><li>yahoo.com</li></ul><hr /><p>Note for experts: if you can use IPv6, try upgrading to 0.2.3.12-alpha and use this IPv6 address in your bridge line:<br /><tt>[2001:948:7:2::164]:6001</tt><br />Let us know how it goes!</p></body></html>
提取结果:
83.226.165.182:443
78.147.155.162:9001
86.204.237.150:443
网上搜索到的只能提取IP

代码: 全选

sed -n 's,^[^ ]*[ ]\([0-9][0-9]*\(\.[0-9][0-9]*\)\{3\}\):.*$,\1,p' test
提取结果少了一个

代码: 全选

awk  '{gsub(/[^0-9.]/," ",$0);for(i=1;i<=NF;i++) if($i~/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) print $i}' test
如何修改或者新的命令来提取出123.456.789.012:9001此种格式的结果
不懂sed和awk,请帮忙.
谢谢2楼的快速回复解决了我的难题.
上次由 bigsun 在 2012-06-27 20:47,总共编辑 1 次。
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: [求助]如何提取文件中出现的ip:port

#2

帖子 lilydjwg » 2012-06-27 20:40

代码: 全选

grep -oP '\b(\d+\.){3}\d+:\d+\b'
回复