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

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

问题描述

我有被制表符分隔的大量数据的文本文件。我想看看数据,这样我可以看到一列中唯一值。例如,

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.

补遗:感谢大家的帮助,我能问一件事?如果我想这些独特值的计数呢?

我想我没有把第二部分不够清楚。我想要做的是有这些独特价值的每个计数不知道的唯一值究竟有多少。例如,在第一列我想知道有多少红,蓝,绿等颜色的物体在那里。

推荐答案

您可以使用排序 uniq的命令如下:

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

cat input_file | cut -f 1 | sort | uniq

获取唯一值的字段1,2 1更换会给你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

编辑:

要算独特OCCURENCES的指挥链的数量,您可以使用的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天全站免登陆