开发者

How to know what to

开发者 https://www.devze.com 2023-03-16 23:29 出处:网络
Im using awk, and want to send in an arbitrary number of text files (arguments) into my script, and for each input file \"print something\".

Im using awk, and want to send in an arbitrary number of text files (arguments) into my script, and for each input file "print something".

I have found the script here

And modified the script into this

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

Problem is what do I write in nfiles="10" since its supposed to be arbitrary number of text arguments?

Also I quite don't understand this script, it only executes this section

 else if(a[i]==1) {
        print i > "output3"
    }

And only prints out the information from file1.txt. Why don't it executes the rest, and what if I would like to print out everything in row 3 ($3) from all files into output3? Thanks =)


If you want to print the third column $3 of every file into a single output file, you could use something like this:

awk '{ print $3 }' file1 [file2 .. filen] > output3

If you want to print the third line of every file:

awk 'FNR == 3' file1 [file2 .. filen] > output3

You can, of course, use glob to match your files:

awk '{ print $3 }' common_pattern* > output3


Instead of passing nfiles=10 add this to your awk script as the first action.

FNR==1 {nfiles++}


Use to calculate the number of files added:

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

精彩评论

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