views:

1166

answers:

6

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
+1  A: 

You can do it this way too:

find . -name '*txt' -print -exec awk 'BEGIN {nl=1 ;print FILENAME} $9 !="" {if (nl<11) { print $0 ; nl = nl + 1 }}' {}  \;

without head.

Zsolt Botykai
@Zsolt: I tried. For each file, your snippet keeps going on even after 10th line.
neversaint
@Zsolt: thanks. How can I make it to print the file name being (as delimiter) also?
neversaint
+4  A: 

When running a command with find's -exec, you don't get all the nice shell things like the pipe operator (|). You can regain them by explicitly running a subshell if you like though, eg:

find . -name '*.txt' -exec /bin/sh -c "echo a text file called {} | head -n 15" \;

Anthony Towns
+1  A: 

Using awk only should work:

find . -name "*.txt" -print -exec awk '{if($9!=""&&n<11){print;n++}}' {} \;
mouviciel
@mouviciel: thanks. How can I make it to print the file name being processed also?
neversaint
It does: this is the -print flag of your find command.
mouviciel
@mouviciel: thanks, got it.
neversaint
+3  A: 

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++}' {} \;
ashawley
+2  A: 

Based on Ashawley's answer:

find . -name "*.txt" -print -exec awk '$9 != "" {print; if(NR > 9) exit; }' {} \;

It should perform better, as we exit awk after the 10th record.

MatthieuP
It exits after the tenth record, but not after the tenth printed line which is what the OP wanted and ashawley's answer provides.
Dennis Williamson
A: 

I'm getting the following error. does any one knoe how to fix it

$ sh skipper.sh file1 filea fileb filec filec fileb filea fileb filec fileb awk: cmd. line:2: (FILENAME=filec FNR=7) fatal: cannot open file `file10' for re ading (No such file or directory)

here is my code

awk -v nfiles="10" 'NR==FNR{a[$0]++;next}
$0 in a {a[$0]++; next}
{b[$0]++}
END{
  for(i in a){
    if(a[i]==nfiles) {
      print i > "output1"
    }
    else if(a[i]==1) {
        print i > "output3"
    }
  }
  for(i in b){
    if(b[i]==nfiles-1) {
        print i > "output2"
    }
  }
}' $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
repinementer