将每个第一列元素放在第二列上出现的次数 [英] Put each first column element the number of times that appear on second column

查看:32
本文介绍了将每个第一列元素放在第二列上出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有太多使用 Unix 工具的经验,我想知道如何做到这一点:

I don't have much experience using Unix tools and I was wondering how to do this:

我有一个像这样有 2 列的文件(空格标签):

I have a file with 2 columns like this (space tab):

Agent 2
Person 3
Place 1
Location 4

第一列的每个不同元素都是一个数字(Agent -> 1, Person -> 2, Place -> 3code>, 位置 -> 4).

Each different element of first column will be a number (Agent -> 1, Person -> 2, Place -> 3, Location -> 4).

因此,我想让每个第一列数字元素出现在第二列上的次数.在这种情况下:

Thus, I want to have each first column numeric element the number of times that appear on the second column. In this case:

1
1
2
2
2
3
4
4
4
4

说明:Agent(1)出现2次,Person(2)出现3次,以此类推

Explanation: Agent (1) appears 2 times, Person (2) appears 3 times, etc.

希望你能帮助我.提前致谢.

Hope you can help me. Thanks in advance.

推荐答案

$ awk '{for (i=1; i<=$2; i++) print NR}' file
1
1
2
2
2
3
4
4
4
4

这篇关于将每个第一列元素放在第二列上出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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