SED / AWK +正则表达式删除重复的行,其中第一场比赛(IP地址) [英] sed/awk + regex delete duplicate lines where first field matches (ip address)

查看:652
本文介绍了SED / AWK +正则表达式删除重复的行,其中第一场比赛(IP地址)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个解决方案来删除重复的行,其中第一场是IPv4 address.For比如我有一个文件中的以下行:

I need a solution to delete duplicate lines where first field is an IPv4 address.For example I have the following lines in a file:

192.168.0.1/text1/text2
192.168.0.18/text03/text7
192.168.0.15/sometext/sometext
192.168.0.1/text100/ntext
192.168.0.23/othertext/sometext

因此​​,所有它在previous场景相匹配的是IP地址。我所知道的是,IP地址的正则表达式是:

So all it matches in the previous scenario is the IP address. All I know is that the regex for IP address is:

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

如果该解决方案是一行,并尽可能快地这将是很好。

It would be nice if the solution is one line and as fast as possible.

推荐答案

如果,该文件所包含的行在您显示格式,即第一场始终是IP地址,就可以逃脱1号线的awk:

If, the file contains lines only in the format you show, i.e. first field is always IP address, you can get away with 1 line of awk:

awk '!x[$1]++' FS="/" $PATH_TO_FILE

编辑:这消除了对基于IP地址的重复的。我不知道这是OP想要的东西时,我写了这个答案。

This removes duplicates based only on IP address. I'm not sure this is what the OP wanted when I wrote this answer.

这篇关于SED / AWK +正则表达式删除重复的行,其中第一场比赛(IP地址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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