我有一个这种格式的横幅列表:
Hostname: []
IP: xxx.xxx.xxx.xxx
Port: xx
HTTP/1.0 301 Moved Permanently
Location: /login.html
Content-Type: text/html
Device-Access-Level: 255
Content-Length: 3066
Cache-Control: max-age=7200, must-revalidate
我使用了以下grep语句来获取ip:
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
为了获取端口,我还需要添加到语句中? (虽然仍然获得IP。)。
感谢您的回答..!
答案 0 :(得分:2)
为什么不使用awk
awk '/IP:/ {ip=$2} /Port:/ {print ip,$2}' file
当找到IP:
行时,它会将IP存储在变量ip
中
找到端口时,请打印ip
和端口号。
实施例
cat file
Hostname: []
IP: 163.248.1.20
Port: 843
HTTP/1.0 301 Moved Permanently
Location: /login.html
Content-Type: text/html
Device-Access-Level: 255
Content-Length: 3066
Cache-Control: max-age=7200, must-revalidat
awk '/IP:/ {ip=$2} /Port:/ {print ip,$2}' file
163.248.1.20 843