如何在 bash 中处理每个第二个文件?
我有一个包含几十个文件的目录。我想对该目录中的每个第二个文件执行一些操作。到目前为止,我只使用了 find
命令,但我用这个命令处理所有文件:
find ./dir/ -type f -exec cat {} \;
I have a directory with a few dozens of files. I would like to do something with every second file from this directory. By now I only used find
command but with this I process all files:
find ./dir/ -type f -exec cat {} \;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NR
是当前行号。要获取奇数行,请使用... == 1
。NR
is the current row number. To get odd rows, use... == 1
.或者
or
如果您想在每个备用文件上运行 my_cmd,这可能会有所帮助,
我已从 如何使用 sed 删除所有其他行?
If you want to run my_cmd on each of the alternate file, this may help
I've copied the sed from How to remove every other line with sed?
每个文件我都有两次,并且需要删除每个文件。 Find 只是返回给我随机文件,因此我添加了排序。现在看起来像这样:
将其放入名为 scriptName 的文件中,
通过调用运行并启动它
I had every file twice, and needed to delete every second file. Find just returned me random files, therefore I added a sort. It now looks like this:
put this in a file called scriptName, run
and start it by calling