Linux中的数据处理 [英] Data manipulation in linux

查看:86
本文介绍了Linux中的数据处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据某些条件过滤或删除文本文件中的某些行(尝试使用awk,但未成功). 我有一个文件,其中包含一些用逗号,分隔的列.这样的文件的一个示例是:

I am trying to filter or remove some lines in a text file based on some criteria (tried with awk, but no success). I have a file that contains some columns separated by a comma ,. An example of such a file is:

source,destination
192.168.1.2,8.8.8.8
8.8.8.8,192.168.1.2

我有兴趣删除或过滤出信息相同的那些行.

I am interested to remove or filter out those lines where the information is the same.

因此,如果文件包含相反的源目标:

so if the file contains the reversed source destination:

192.168.1.2,8.8.8.8
8.8.8.8,192.168.1.2

然后仅显示其中一行,而不是全部显示.

then only show one of the lines, not both.

推荐答案

您可以尝试执行此操作,但是如果文件很大,请小心,因为它将值保留在内存中.

You can try this but be careful if the file is huge as it keeps the values in memory.

awk -F, '!($1 FS $2 in dup){dup[$1 FS $2]=dup[$2 FS $1]; print}' <file>

相同的想法:

awk -F, '!(($1 FS $2 in dup)||($2 FS $1 in dup)){dup[$1 FS $2]; print}' <file>

这篇关于Linux中的数据处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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