在C-shell中打印具有相同扩展名的总金额文件 [英] Print total amount file which have the same extension in C-shell

查看:63
本文介绍了在C-shell中打印具有相同扩展名的总金额文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件包括:

rpt
rpt
rpt
qor
rpt
qor

我希望它们出现在路径下面:

I want them to appear inside below path:

rpt       4
qor       2

推荐答案

使用 ls 和awk:

$ ls | awk -F'.' -v OFS='\t' 'NF>1{a[$NF]++}END{for(i in a)print i,a[i]}'

解释:

$ ls |                    # ls to feed files
awk -F'.' -v OFS='\t' '   # set field delimiters
NF>1 {                    # only consider filenames with period as ext. sep.
    a[$NF]++              # hash extension and keep count
}
END {                     # after processing ls output
    for(i in a)           # output results
        print i,a[i]
}'

注意,解析 ls 输出不是最明智的方法,因为如果文件名包含换行符或其他特殊字符,则结果可能不真实.在这种情况下,您可以使用以下GNU awk:

Notice that parsing ls output is not the most sane way since if filenames have newlines or other special characters, the outcome may not be truthful. In that case you can use this GNU awk:

$ gawk -v OFS='\t' '
BEGINFILE {
    if(FILENAME~/\./) {           # double checking after the *.* 
        n=split(FILENAME,t,/\./)  # get that extension
        a[t[n]]++                 # hash and count
    }
    nextfile                      # sjip to net files
}                                 # excuse my dutch above: skip to next file
END {                             # see above
    for(i in a)
        print a[i],i
}' *.*                            # filter filenames without periods here

这篇关于在C-shell中打印具有相同扩展名的总金额文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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