需要计数器 find 和 xarg 组合

Need counter on find and xarg combo

所以我有这个代码:

find cobacoba -type f | xargs -n 5 bash -c 'a=([=10=]    ); echo "File #: ${a[*]}";'

希望结果:

File #: cobacoba/1.3 cobacoba/1.6 cobacoba/1.q cobacoba/1.5
File #: cobacoba/1.1 cobacoba/1.q2 cobacoba/1.q23 cobacoba/1.4
File #: cobacoba/1.2

我想用计数器代替#,比如1、2、3等等...

您可以使用 awk 对输出进行后处理,以将 # 替换为行号:

find cobacoba -type f |
   xargs -n 5 bash -c 'a=([=10=]    ); echo "File #: ${a[*]}";' |
   awk '{gsub("#", NR, [=10=]); print}'

此方法的来源:Sed replace pattern with line number

PS:有人可能会争辩说我应该将问题标记为重复而不是回答,但我认为即使解决方案相同,上下文也有很大不同以保证答案。