首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"find \ awk“命令(Linux)所需的解释

"find \ awk“命令(Linux)所需的解释
EN

Stack Overflow用户
提问于 2020-08-24 16:04:27
回答 2查看 82关注 0票数 1

我正在试图找出下面的命令;

代码语言:javascript
复制
find ./ -type f |
awk -F / -v OFS=/ '{$NF="";dir[$0]++} END {for (i in dir) print dir[i]i}'

输出如下:

代码语言:javascript
复制
6./Release_1_18_1_0_06_26/metadata/3_Control_Files/   
5./Release_1_18_1_0_06_26/metadata/7_SAS_Code/   
5./Release_1_18_1_0_06_26/others/1_content/   
1./.cache/pip/selfcheck/   
2./Release_1_18_1_0_06_26/metadata/5_Status/   
1./Release_1_18_1_0_06_26/compute/2_packages/   
1./sasuser.v94/   
4./metadata/FR1_Release_1.17.1.1/3_Control_Files/   
4./Release_1_18_1_0_06_26/metadata/6_Patches/   

此命令正在计算当前路径中的代码数。但是,我不理解{$NF="";dir[$0]++} END {for (i in dir) print dir[i]i},特别是dir[$0]。有人能解释吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-24 16:27:15

请您在这里详细解释OP的代码,好吗?

代码语言:javascript
复制
find ./ -type f |     ##Running find command to find files in current directory and passing output as input to awk command.
awk -F / -v OFS=/ '   ##Running awk command setting field separator and output field separator as /
{
  $NF=""              ##Nullifying last field of current line here.
  dir[$0]++           ##creating array dir with current line and keep increasing its value with one here.
}
END{                  ##starting END block of this code here.
  for(i in dir){      ##traversing through dir array here.
    print dir[i]i     ##printing index of dir array and it's index here.
  }
}'

当最终完成NOTE:END读取时,将执行任何awk代码的Input_file块。

票数 3
EN

Stack Overflow用户

发布于 2020-08-24 16:31:57

-F /告诉awk用斜线分隔行。$0是整条线。

$NF=""将行中的最后一项(在本例中为文件名)替换为空。

然后,dir[$0]++取整行(在文件名被删除后),并将其用作哈希索引,将该值递增1。有效地计算具有相同路径的项目数。

END块循环遍历dir[]哈希打印中的所有键,首先是计数,然后是目录名。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63564657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档