To sum it up....

This works:

awk 'BEGIN { FS=":"; }
{ print  $1  $3  $5; }' /etc/passwd

But this doesn't:

awk 'BEGIN { FS=":"; }
{ echo  $1  $3  $5; }' /etc/passwd

I would like to know why.

  • 1
    print is a keyword in Awk; echo is a shell word. Your examples are Awk examples that happen to be run in a shell... – jasonwryan Mar 24 '16 at 4:34
  • 3
    1. because awk doesn't have an echo keyword, it has print instead. 2. because awk and sh (or bash/dash/zsh/ksh etc. and perl. and python) are different scripting languages and have different keywords and syntax. – cas Mar 24 '16 at 5:15

The structure of awk execution is: pattern { action statements }

    Action statements are enclosed in braces, { and }. Action statements consist of the usual assignment, conditional, and looping statements found in most languages. The operators, control statements, and input/output statements available are patterned after those in C.

  • print is I/O statement of awk.

    From manpage:

    print                 Print the current record.  The output record is terminated with the value of ORS.
  • Visit manual>The print Statement:

    The print Statement

    Use the print statement to produce output with simple, standardized formatting. You specify only the strings or numbers to print, in a list separated by commas. They are output, separated by single spaces, followed by a newline. The statement looks like this:

    print item1, item2, …

Visit man awk for more details.

Also note that:

       AWK is a line-oriented language.  The pattern comes  first,  and  then  the  action.   Action  statements  are
       enclosed  in  {  and  }.  Either the pattern may be missing, or the action may be missing, but, of course, not
       both.  If the pattern is missing, the action is executed for every single record of input.  A  missing  action
       is equivalent to

              { print }

       which prints the entire record.

awk doesn't have echo keyword/statement.

$ man awk | grep echo | wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.