开发者

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 > "o开发者_如何学Goutput2"
    }
  }
}' $1 $2 $3 $4 $5 $6 $7 $8 $9 $10


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

${10}


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}
...
} "$@"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜