在Unix上按行/字段计数字符的出现次数 [英] Count occurrences of character per line/field on Unix

查看:96
本文介绍了在Unix上按行/字段计数字符的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个包含这样数据的文件(即stores.dat文件)

Given a file with data like this (ie stores.dat file)

sid|storeNo|latitude|longitude
2tt|1|-28.0372000t0|153.42921670
9|2t|-33tt.85t09t0000|15t1.03274200

返回每行出现't'字符的次数的命令是什么?

What is the command that would return the number of occurrences of the 't' character per line?

例如会返回:

count   lineNum
   4       1
   3       2
   6       3


此外,要按字段的出现次数进行计数,返回以下结果的命令是什么?


Also, to do it by count of occurrences by field what is the command to return the following results?

例如输入第2列和字符"t"

eg. input of column 2 and character 't'

count   lineNum
   1       1
   0       2
   1       3

例如输入第3列和字符"t"

eg. input of column 3 and character 't'

count   lineNum
   2       1
   1       2
   4       3

推荐答案

要计算每行字符的出现,您可以执行以下操作:

To count occurrence of a character per line you can do:

awk -F'|' 'BEGIN{print "count", "lineNum"}{print gsub(/t/,"") "\t" NR}' file
count lineNum
4       1
3       2
6       3

要计算每个字段/列中某个字符的出现次数,您可以执行以下操作:

To count occurrence of a character per field/column you can do:

第2列:

awk -F'|' -v fld=2 'BEGIN{print "count", "lineNum"}{print gsub(/t/,"",$fld) "\t" NR}' file
count lineNum
1       1
0       2
1       3

第3列:

awk -F'|' -v fld=3 'BEGIN{print "count", "lineNum"}{print gsub(/t/,"",$fld) "\t" NR}' file
count lineNum
2       1
1       2
4       3

  • gsub()函数的返回值是进行替换的次数.所以我们用它来打印数字.
  • NR保存行号,因此我们使用它来打印行号.
  • 对于特定字段的打印事件,我们创建一个变量fld并放入要从中提取计数的字段编号.
    • gsub() function's return value is number of substitution made. So we use that to print the number.
    • NR holds the line number so we use it to print the line number.
    • For printing occurrences of particular field, we create a variable fld and put the field number we wish to extract counts from.
    • 这篇关于在Unix上按行/字段计数字符的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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