repinementer
repinementer

Reputation: 21

Error processing multiple files

I'm trying to process 10 files with awk in my script, and I'm getting the following error.

$ 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 reading (No such file or directory)

Does any one know how to fix it? It is working fine with <10 files but I need to use it 10 or 50 files.

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

Upvotes: 2

Views: 223

Answers (2)

TrueY
TrueY

Reputation: 7610

With a little modification of your code you can use any number of files, you need

awk 'BEGIN{nfiles=ARGC-1}
...
} "$@"

In this way You can enter any number of files to your awk script. It will process the empty files as well. If you want to skip the empty files You can use this:

awk 'FNR==1{++nfiles}
...
} "$@"

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342649

When you hit argument 10 and above, you should use braces eg

${10}

Upvotes: 5

Related Questions