Ilion
Ilion

Reputation: 6882

using pipes with a find command

I have a series of delimited files, some of which have some bad data and can be recognized by doing a column count on them. I can find them with the following command:

find ./ -name 201201*gz -mtime 12

They are all gzipped and I do not want to un-archive them all. So to check the column counts I've been doing I'm running this as a second command on each file:

zcat ./path/to/file.data | awk '{print NF}' | head

I know I can run a command on each file through find with -exec, but how can I also get it to run through the pipes? A couple things I tried, neither of which I expected to work and neither of which did:

find ./ -name 201201*gz -mtime 12 -print -exec zcat {} \; | awk '{print NF}'| head
find ./ -name 201201*gz -mtime 12 -print -exec "zcat {} | awk '{print NF}'| head" \;

Upvotes: 2

Views: 2397

Answers (2)

Johannes Weiss
Johannes Weiss

Reputation: 54081

I'd use a explicit loop aproach:

find . -name 201201*gz -mtime 12 | while read file; do
    echo "$file: "
    zcat "$file" | awk '{print NF}' | head
done

Upvotes: 3

bensnider
bensnider

Reputation: 3772

More or less you pipe things through find like:

find . -name "foo" -print0 | xargs -0 echo

So your command would look like:

find ./ -name "201201*gz" -mtime 12 -print0 | xargs -0 zcat | awk '{print NF}'| head

-print0 and xargs -0 just helps to make sure files with special characters dont break the pipe.

Upvotes: 2

Related Questions