omer aizerner
omer aizerner

Reputation: 31

How to run an awk script on a certain file without putting the file name when i run the program

Is it possible to make an awk program to run on a file without giving it the name of the file when you run the program.

i tried to do this but for some reason it does not work,can someone please explain why this does not work and how do i make it work

#!/bin/awk -f
BEGIN{
ARGV[1]="books"
}
{
split($0,a,":")
if(a[4]!="-"){
n[a[2]]=n[a[2]]+1
print $0","n[a[2]]
}
}
END{
for(x in n){
print n[x]
}
}

in this program i tried to make it run on a file name books but it still does not work

Upvotes: 2

Views: 107

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74685

As well as adding an element to ARGV, you need to increment ARGC:

#!/bin/awk -f
BEGIN{
  ARGV[1]="books"
  ++ARGC
}

From the GNU AWK manual:

Storing additional elements [in ARGV] and incrementing ARGC causes additional files to be read.

Upvotes: 5

Related Questions