0

I know that awk is a script programming language, but sometimes I get confused about when I interpret as command or as a program.

Eg. 1 - Here I interpret as a command:

awk '{print $2}' file.txt

Eg. 2 - Here I interpret as a awk program:

awk 'BEGIN      {skip = 0} \
        skip == 0  {if (NF == 0) 
                     {skip = 1}  \
                    else 
                     {print};  \
                    next} \
        skip == 1  {print; \
                    skip = 0; \
                    next}'

Taken from here.

The questions are:

  1. When awk need be interpreted as a command?
  2. When awk is a program?
  3. There's a problem call my first example as a awk command?

Something like:

awk '{print $2}' file.txt | awk '{FS=" "} {print 4}'

means that awk programs can communicate using pipes?

1
  • 2
    Your question doesn't really make a lot of sense. Most commands are programs (with notable exceptions); most programs are commands. awk, like cat and ls, is both a command and a program. The awk program/command is an interpreter for a language which, apparently, is called "AWK". The string following the awk command -- even if it's as little as {print $2} -- is an AWK program. If you say awk -f foo, then the file foo (or, arguably, its content) is an AWK program. And yes, just like (most) other commands/programs, awk processes can have their I/O redirected to files and pipes. Oct 16, 2014 at 17:48

2 Answers 2

4

awk has two modes of invocation one is with a program text on the command line and the other with a program from a file. This is stated in the synopsis of the awk man page (this one from mawk on Ubuntu 12.04):

   mawk  [-W  option] [-F value] [-v var=value] [--] 'program text' [file
   ...]
   mawk [-W option] [-F value]  [-v  var=value]  [-f  program-file]  [--]
   [file ...]

Whether to call the first form a program or not depends on the definition of program you want use. I would say both forms involve programs, whereby on the first the program is specified as a commandline argument. Both of your examples to be of the first form, as neither involves the -f option. That the second example has a multiline commandline argument is, with respect to this, irrelevant.

This is not unique to awk. e.g. python by defaults interprets a commandline argument as a program name, but with the -c option allows you to specify a program on the commandline (i.e. the default is the other way around from awk).

Independent of that is the communication using pipes. That is taken care of by the shell syntax and the OS, the only thing your script needs to do is write to stdout, reps. read from stdin. So yes awk programs can communicate over pipes.

1
  • Thanks @Anthon (including the edition). Growing and learning.
    – Cold
    Oct 16, 2014 at 10:27
3

Strictly speaking, when you call awk, you refer to the interpreter, not the language. The language is called AWK.

awk (or mawk, nawk) is only utility, which will execute programs written in AWK programming language.

As POSIX defined, awk program is:

program

If no -f option is specified, the first operand to awk shall be the text of the awk program. The application shall supply the program operand as a single argument to awk. If the text does not end in a , awk shall interpret the text as if it did.

So if you don't use -f option, you can consider two of your examples as awk program.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .