Need counter on find and xarg combo

て烟熏妆下的殇ゞ 提交于 2019-12-24 17:23:25

问题


So I have this code:

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

Hoping Result:

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

I would like to replace # with counter, like 1, 2, 3, so on...


回答1:


You can postprocess your output with awk to replace # with the line number:

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

My source for this method: Sed replace pattern with line number

PS: one might argue I should have flagged the question as a duplicate instead of answering, but I think the context is sufficiently different to warrant an answer even if the solution is the same.



来源:https://stackoverflow.com/questions/57450254/need-counter-on-find-and-xarg-combo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!