sed - 如何使用 sed 提取 IP 地址? [英] sed - How to extract IP address using sed?

查看:112
本文介绍了sed - 如何使用 sed 提取 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个作业,所以我别无选择,只能使用 sed.

This is for an assignment so I have no choice but to use sed.

给定一个文件 messages,我如何提取所有 IP 地址并打印它们?

Given a file messages, how can I extract all the IP addresses and print them?

我第一次尝试

sed -n '/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' messages

但它什么也没打印.在做了一些研究之后,我发现 sed 不支持像 ?和|.

But it printed nothing. After doing some research, I found out that sed does not support non-greedy operators like ? and |.

我一直在绞尽脑汁,但我想不出没有非贪婪操作符的方法来做到这一点.我该怎么做?

I've been wracking my brain but I can't think of a way to do this without the non-greedy operators. How can I do this?

推荐答案

使用 sed -r (extended regex) 或使用 \

Use sed -r (extended regex) or escape the capture groups with \

这篇关于sed - 如何使用 sed 提取 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆