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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe structure of awk
execution is: pattern { action statements }
Actions
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:
PATTERNS AND ACTIONS
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
0
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:34awk
doesn't have anecho
keyword, it hasprint
instead. 2. becauseawk
andsh
(orbash
/dash
/zsh
/ksh
etc. andperl
. andpython
) are different scripting languages and have different keywords and syntax. – cas Mar 24 '16 at 5:15