linuxunixawkfindunix-head

Applying Unix's Head on AWK Through Find Command


I want to output top 10 lines of AWK command in the list of files given by find, using this snippet:

$ find . -name "*.txt" -print -exec awk '$9 != ""'  \| head -n10 {} \;

Note also that I want to print out the file names being processed.

But why I get such error:

awk: cmd. line:2: fatal: cannot open file `|' for reading (No such file or directory)
./myfile.txt

What's the right way to do it?

I tried without backslash before the pipe. Still it gave an error:

find: missing argument to `-exec'
head: cannot open `{}' for reading: No such file or directory
head: cannot open `;' for reading: No such file or directory

Solution

  • If you want to run an Awk program on every file from find that only prints the first 10 lines each time.

    $ find . -name "*.txt" -print -exec awk '$9 != "" && n < 10 {print; n++}' {} \;