find results piped to zcat and then to head

furedde picture furedde · Jul 27, 2010 · Viewed 9.8k times · Source

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.

Answer

Dennis Williamson picture Dennis Williamson · Jul 27, 2010

You should find that this will work:

find . -name "*.gz" | while read -r file; do zcat -f "$file" | head -n 1; done