在find和xarg组合上需要计数器 [英] Need counter on find and xarg combo

查看:80
本文介绍了在find和xarg组合上需要计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码:

find cobacoba -type f | xargs -n 5 bash -c 'a=(${0} ${1} ${2} ${3} ${4}); 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,依此类推...

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

推荐答案

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

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}'

此方法的来源: Sed替换模式为行号

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

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.

这篇关于在find和xarg组合上需要计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆