Chen
Chen

Reputation: 75

How to run an .awk file without 'awk -f' command?

I am new to awk script. I am trying to figure out how to run an awk file without awk -f command. I see people keep saying add "#!bin/awk -f" for the first line of an awk file. But this didn't for my awk. It still gives me "no file or directory" error. I question is what does "#!bin/awk -f" really mean, and what does it do?

Upvotes: 1

Views: 472

Answers (3)

Addison
Addison

Reputation: 3949

Its #!/bin/awk -f not #!bin/awk. That will probably work, but theres no guaranty. If someone who has awk installed in a different location runs your script, it won't work. What you want is this: #!/usr/bin/env awk -f.

#! is what tells bash what to use to interpret your script. It should go at the very top of your file. It's called a Shebang. Right after that, you put the path to the interpreter.

/usr/bin/env finds where awk is located, and uses that script as the interpreter. So if they installed awk into somewhere else like /usr/local/bin then it'll find it. This probably won't matter for you, but it's a good habit to get into. It's more portable, and can be shared easier.

The -f says that awk is gonna read from a file. You could do awk -f yourfilename.awk in bash, but in the shebang, -f means the rest of the code will be the file it reads from.

I hope this helped. Feel free to ask me any questions if it doesn't work, or isn't clear enough.

UPDATE

If you get the error message:

/usr/bin/env: ‘awk -f’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

then change the first line of your script to #!/usr/bin/env -S awk -f (tested with GNU bash, version 4.4.23)

Upvotes: 2

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84393

The Shebang for Awk Explained

  • #! is the start of a shebang line, which tells the shell which interpreter to use for the script.
  • /bin/awk is the path to your awk executable. You may need to change this is your awk is installed elsewhere, or if you want to use a different version of awk.
  • -f is a flag to awk to tell it to interpret the flag's argument as an awk script. In a shebang, it tells some awks to interpret the remainder of the script instead of a file.

Your Shebang is (Probably) Broken

You are using #!bin/awk -f which is unlikely to work, unless you have awk installed as $PWD/bin/awk. You probably meant to use #!/bin/awk instead.

In some instances, passing a flag on the shebang line may not work with your shell or your awk. If you have the rest of the shebang line correct, you might try removing the -f flag and see if that works for you.

Upvotes: 0

John C
John C

Reputation: 4396

You probably want

#!/bin/awk -f 

(The first slash after the #! is important).

This tells unix what program it should use to 'run' the script with.

It is usually called the 'shebang' which comes from hash + bang.

If you want to run your script like this you need to make sure it is executable (chmod +x <script>).

Otherwise you can just run your script by typing the command /bin/awk -f <script>

Upvotes: 0

Related Questions