查找具有重复模式的行

查找具有重复模式的行

我有一个这样的日志文件

123.123.123.123 (456.456.456.456) abc def .....
123.123.123.123 (123.123.123.123) abc def .....
123.123.123.123 (456.456.456.456) abc def .....
455.455.455.455 (456.456.456.456) abc def .....
555.555.555.555 (555.555.555.555) efc bbb .....

在某些行中,括号外面的IP与里面的相同,我想输出这些行:

123.123.123.123 (123.123.123.123) abc def .....
555.555.555.555 (555.555.555.555) efc bbb .....

我怎样才能用 grep 或 sed 做到这一点?

答案1

您可以使用反向引用到一个编号的捕获组

$ grep '\([0-9.]*\) (\1)' file.log
123.123.123.123 (123.123.123.123) abc def .....
555.555.555.555 (555.555.555.555) efc bbb .....

相关内容