简体   繁体   English

Grep / Sed / Awk在特定列中修剪IP地址周围的文本以仅保留IP本身?

[英]Grep/Sed/Awk Trim text around IP address in specific column to just leave the IP itself??

I've been struggling with this issue for quite some time, I'm a beginner in using tools like grep/sed/awk/cut, as well as a beginner in regular expressions. 我已经为这个问题苦苦挣扎了一段时间,我是使用grep / sed / awk / cut等工具的初学者,也是使用正则表达式的初学者。 I need to parse out a Cisco ASA Firewall log so that "columns" that contain an IP address are trimmed where only the IP address is left in its place. 我需要解析出一个Cisco ASA防火墙日志,以便对包含IP地址的“列”进行修整,仅保留IP地址。 Please see the following example. 请参见以下示例。

This: 这个:

Built inbound ICMP connection for faddr [hostname_here]-[ip address]/[port] gaddr [ip address]/[port] laddr [ip address]/[port]

Needs to be parsed out to this: 需要对此进行解析:

Built inbound ICMP connection for faddr [ip address] gaddr [ip address] laddr [ip address]

Honestly I don't think it's worth it to post what I've done so far because I'm sure I'm approaching this the wrong way. 老实说,我认为发布我到目前为止所做的事情是不值得的,因为我敢肯定我会以错误的方式来做。

I really appreciate your help. 非常感谢您的帮助。

With GNU sed: 使用GNU sed:

sed -rn  's/([fgl]addr )([^ -]*[ -])?(([0-9]{1,3}\.){3}[0-9]{1,3})\/[0-9]*/\1\3/gp' file

With file containing: file包含:

Built inbound ICMP connection for faddr google.com-129.244.54.55/63 gaddr 9.9.123.33/25 laddr 111.87.75.0/8444

Output: 输出:

Built inbound ICMP connection for faddr 129.244.54.55 gaddr 9.9.123.33 laddr 111.87.75.0

Try: 尝试:

egrep -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" firewall.log

Source: Regular expression to match DNS hostname or IP Address? 来源: 正则表达式以匹配DNS主机名或IP地址?

To add some custom text for each of the addresses, add to above: 要为每个地址添加一些自定义文本,请在上面添加:

| while read ip; do echo "Built inbound ICMP connection for faddr $ip gaddr $ip laddr $ip"; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM