我正在试图找出下面的命令;
find ./ -type f |
awk -F / -v OFS=/ '{$NF="";dir[$0]++} END {for (i in dir) print dir[i]i}'输出如下:
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]。有人能解释吗?
发布于 2020-08-24 16:27:15
请您在这里详细解释OP的代码,好吗?
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块。
发布于 2020-08-24 16:31:57
-F /告诉awk用斜线分隔行。$0是整条线。
$NF=""将行中的最后一项(在本例中为文件名)替换为空。
然后,dir[$0]++取整行(在文件名被删除后),并将其用作哈希索引,将该值递增1。有效地计算具有相同路径的项目数。
END块循环遍历dir[]哈希打印中的所有键,首先是计数,然后是目录名。
https://stackoverflow.com/questions/63564657
复制相似问题