如何计算制表符分隔的文本文件中字段的唯一值的数量? [英] How to count number of unique values of a field in a tab-delimited text file?

查看:262
本文介绍了如何计算制表符分隔的文本文件中字段的唯一值的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量数据的文本文件,它是制表符分隔的。我想看看数据,以便我可以看到一个列中的唯一值。例如,

I have a text file with a large amount of data which is tab delimited. I want to have a look at the data such that I can see the unique values in a column. For example,

Red     Ball 1 Sold
Blue    Bat  5 OnSale
............... 

所以,它像第一列有颜色,所以我想以了解该列中有多少个不同的唯一值,并且我希望能够为每个列执行此操作。

So, its like the first column has colors, so I want to know how many different unique values are there in that column and I want to be able to do that for each column.

我需要在Linux命令行中执行此操作,所以可能使用一些bash脚本,sed,awk或某物。

I need to do this in a Linux command line, so probably using some bash script, sed, awk or something.

附录:感谢大家的帮助,我可以问一件事吗?如果我想要计数这些唯一的值,怎么办?

我想我没有把第二部分放得很清楚。我想做的是有一个每个这些唯一值的计数不知道有多少唯一值。例如,在第一列中,我想知道有多少红,蓝,绿等彩色对象。

推荐答案

您可以使用 cut sort uniq 命令如下:

You can make use of cut, sort and uniq commands as follows:

cat input_file | cut -f 1 | sort | uniq

在字段1中获取唯一值,将1替换为2将在字段2中给出唯一值。

gets unique values in field 1, replacing 1 by 2 will give you unique values in field 2.

避免 UUOC :)

cut -f 1 input_file | sort | uniq

编辑

要计算唯一出现次数,您可以在链中使用 wc 命令:

To count the number of unique occurences you can make use of wc command in the chain as:

cut -f 1 input_file | sort | uniq | wc -l

这篇关于如何计算制表符分隔的文本文件中字段的唯一值的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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