Grep/Sed/Awk Trim 特定列中 IP 地址周围的文本仅保留 IP 本身?
Grep/Sed/Awk Trim text around IP address in specific column to just leave the IP itself??
我已经为这个问题苦苦挣扎了一段时间,我是使用 grep/sed/awk/cut 等工具的初学者,也是正则表达式的初学者。我需要解析 Cisco ASA 防火墙日志,以便修剪包含 IP 地址的 "columns",只保留 IP 地址。请看下面的例子。
这个:
Built inbound ICMP connection for faddr [hostname_here]-[ip address]/[port] gaddr [ip address]/[port] laddr [ip address]/[port]
需要解析为:
Built inbound ICMP connection for faddr [ip address] gaddr [ip address] laddr [ip address]
老实说,我认为 post 我到目前为止所做的事情不值得,因为我确定我正在以错误的方式接近它。
非常感谢您的帮助。
使用 GNU sed:
sed -rn 's/([fgl]addr )([^ -]*[ -])?(([0-9]{1,3}\.){3}[0-9]{1,3})\/[0-9]*//gp' file
与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
输出:
Built inbound ICMP connection for faddr 129.244.54.55 gaddr 9.9.123.33 laddr 111.87.75.0
尝试:
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
来源:Regular expression to match DNS hostname or IP Address?
要为每个地址添加一些自定义文本,请添加到上面:
| while read ip; do echo "Built inbound ICMP connection for faddr $ip gaddr $ip laddr $ip"; done
我已经为这个问题苦苦挣扎了一段时间,我是使用 grep/sed/awk/cut 等工具的初学者,也是正则表达式的初学者。我需要解析 Cisco ASA 防火墙日志,以便修剪包含 IP 地址的 "columns",只保留 IP 地址。请看下面的例子。
这个:
Built inbound ICMP connection for faddr [hostname_here]-[ip address]/[port] gaddr [ip address]/[port] laddr [ip address]/[port]
需要解析为:
Built inbound ICMP connection for faddr [ip address] gaddr [ip address] laddr [ip address]
老实说,我认为 post 我到目前为止所做的事情不值得,因为我确定我正在以错误的方式接近它。
非常感谢您的帮助。
使用 GNU sed:
sed -rn 's/([fgl]addr )([^ -]*[ -])?(([0-9]{1,3}\.){3}[0-9]{1,3})\/[0-9]*//gp' file
与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
输出:
Built inbound ICMP connection for faddr 129.244.54.55 gaddr 9.9.123.33 laddr 111.87.75.0
尝试:
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
来源:Regular expression to match DNS hostname or IP Address?
要为每个地址添加一些自定义文本,请添加到上面:
| while read ip; do echo "Built inbound ICMP connection for faddr $ip gaddr $ip laddr $ip"; done