查找结果通过管道传输到 zcat,然后传输到 head
我试图在很多 gzip 压缩的 csv 文件中搜索某个字符串,该字符串位于第一行,我的想法是通过组合 find、zcat 和 head 来获取每个文件的第一行。但我无法让他们一起工作。
$find . -name "*.gz" -print | xargs zcat -f | head -1
20051114083300,1070074.00,0.00000000
xargs: zcat: terminated by signal 13
example file:
$zcat 113.gz | head
20050629171845,1069335.50,-1.00000000
20050629171930,1069315.00,-1.00000000
20050629172015,1069382.50,-1.00000000
.. and 2 milion rows like these ...
虽然我通过编写 bash 脚本、迭代文件并写入临时文件解决了这个问题,但很高兴知道我做错了什么、如何做以及是否有其他方法可以解决这个问题。
I'm trying to search for a certain string in a lot of gziped csv files, the string is located at the first row and my thought was to get the first row of each file by combining find, zcat and head. But I can't get them to work together.
$find . -name "*.gz" -print | xargs zcat -f | head -1
20051114083300,1070074.00,0.00000000
xargs: zcat: terminated by signal 13
example file:
$zcat 113.gz | head
20050629171845,1069335.50,-1.00000000
20050629171930,1069315.00,-1.00000000
20050629172015,1069382.50,-1.00000000
.. and 2 milion rows like these ...
Though I solved the problem by writing a bash script, iterating over the files and writing to a temp file, it would be great to know what I did wrong, how to do it, and if there might be other ways to go about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该发现这可行:
You should find that this will work:
它按照你的要求工作了。
head
完成了它的工作,打印了一行,然后退出。然后,在 xargs 的支持下运行的 zcat 尝试写入关闭的管道,并收到了致命的 SIGPIPE。当它的孩子死掉时,xargs 报告了原因。要获得所需的行为,您需要
find -exec ...
构造或自定义zhead
来提供给 xargs。添加了我在冰箱后面发现的垃圾代码:
它在大约一分钟内处理了 /usr/share/man 中的 10k 个文件。
It worked as you asked it to.
head
did its job, printed one line, and exited.zcat
then running under the auspices ofxargs
tried to write to a closed pipe and received a fatal SIGPIPE for its efforts. Having its child die, xargs reported the whyfor.To get the desired behaviour, you'd need to
find -exec ...
construction or a customzhead
to give to xargs.added junk code I found behind the fridge:
It processed 10k files in /usr/share/man in about a minute.
如果您有 GNU Parallel http://www.gnu.org/software/parallel/安装:
观看 GNU Parallel 的介绍视频,网址为 http://www.youtube.com/watch? v=OpaiGYxkSuQ
If you have GNU Parallel http://www.gnu.org/software/parallel/ installed:
Watch the intro video to GNU Parallel at http://www.youtube.com/watch?v=OpaiGYxkSuQ