第一种解决方案:请您尝试以下方法。
your_git_command |
awk '
match($0,/[^/]*/){
count[substr($0,RSTART,RLENGTH)]++
}
END{
for(i in count){
print i,count[i]
}
}'
说明:为上述添加详细说明。
your_git_command | ##Sending git command output to awk program here.
awk ' ##Starting awk program from here.
match($0,/[^/]*/){ ##Using match function to match regex till / in current line.
count[substr($0,RSTART,RLENGTH)]++ ##Creating array count with index of sub-string from RSTART till RLENGTH with its count increasing with 1
}
END{ ##Starting END block of this code from here.
for(i in count){ ##Traversing through count array here.
print i,count[i] ##Printing i and count value here.
}
}'
第二个解决方案: 上面的解决方案不会关心它们在 Input_file 中的名称顺序,下面的解决方案也会处理它。
your_git_command |
awk '
match($0,/[^/]*/){
val=substr($0,RSTART,RLENGTH)
if(!a[val]++){
b[++count]=val
}
value[val]++
}
END{
for(i=1;i<=count;i++){
print b[i],value[b[i]]
}
}'
说明:为上述添加详细说明,仅供说明之用。
your_git_command | ##Running git command here and sending it to awk command.
awk ' ##Starting awk code from here.
match($0,/[^/]*/){ ##Using match function to match everything till / in current line.
val=substr($0,RSTART,RLENGTH) ##Creating val which has sub-string of current line.
if(!a[val]++){ ##Checking condition if val is NOT coming in a then do following.
b[++count]=val ##Creating array b with index count increasing value of 1 and setting its value to val here.
}
value[val]++ ##Creating array value with index val with its increasing it with 1 value.
}
END{ ##Starting END block of this code here.
for(i=1;i<=count;i++){ ##Running for loop from 1 to till count.
print b[i],value[b[i]] ##Printing array b with variable i as index AND value with index of b[i]
}
}'