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

查看:27
本文介绍了计算 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/,"") "	" 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) "	" NR}' file
count lineNum
1       1
0       2
1       3

第 3 列:

awk -F'|' -v fld=3 'BEGIN{print "count", "lineNum"}{print gsub(/t/,"",$fld) "	" 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天全站免登陆