繁体   English   中英

从文件中提取唯一单词并在 linux 中分隔的同一行选项卡中打印与其关联的数字

[英]Extract unique words from a file and print the numbers associated with it in the same line tab separated in linux

我的文件格式如下:

1: test
18: test
29: test
25: crazy
30: crazy

我想忽略大小写并获取文件中的唯一单词及其各自的计数

所需的 output 应该是:

test: 1 18 29
crazy: 25 30

有人可以指导如何在 Linux/Bash 中做到这一点吗?

有人可以指导如何在 Linux/Bash 中做到这一点吗?

您可以使用 awk 的关联数组来实现:

  • 将第二个字段转换为大写或小写
  • 在 awk 中构建关联数组
  • 将第一步中的结果用作键,并将 append 用作数组中的第一个字段
  • 在所有行都由 awk 处理后,你 go 通过你的数组,打印出键和值

这将打印所需的 output。

awk -F':' '{a[$2]=a[$2]" " $1}END{for(i in a) print i": " a[i]}' input_file.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM