Code:
$ cat <<"EOF" >example1.awk
# Comments begin with # like in shell
# It takes a list of expressions and code blocks, and for every line
# it reads, runs the code blocks in order.
# next will cause it to skip to the next line without running statements below.
{ print "This runs first" }
{ print "This runs second" }
{ print "This runs third"; next }
{ print "This would run fourth except for that next" }
EOF
$ echo | awk -f example1.awk
This runs first
This runs second
This runs third
$ cat <<"EOF" >example2.awk
# Instead of code-blocks, you can put simple statements.
# Whenever the expression is true, the current line will be printed.
# /string/ is a statement. Like in sed, that means to look for string.
/string/
EOF
$ echo "string" | awk -f example2.awk
string
$ echo "strong" | awk -f example2.awk
$ cat <<EOF >example3.awk
# You can put an expression in front of a code block on the same line.
# This will cause it to run the code block only when it is true.
/string/ {
print "String matched"
}
EOF
$ echo "string" | awk -f example3.awk
String matched
$ echo "strong" | awk -f example3.awk
$ cat <<"EOF" >example4.awk
# awk supports variables and columns. $ does NOT mean variable, $ is
# an operator! It means column. So $5 is the 5th column.
# If you did N=5; print $N, that would print the 5th column.
# If the first column is 1, it will print column 1.
# If the first column is 2, it will print column 2. etc.
{ print $($1) }
EOF
$ echo "1 a b c d" | awk -f example4.awk
1
$ echo "4 a b c d" | awk -f example4.awk
c
$ cat <<"EOF" >example5.awk
# BEGIN and END are special code-blocks which run before the program
# begins reading lines and once it finishes reading lines.
BEGIN { print "This runs before" }
END { print "This runs after" }
# This expression is always true, so always prints
1
EOF
$ echo "asdf" | awk -f example5.awk
This runs before
asdf
This runs after
$ cat <<"EOF" >example6.awk
# awk uses spaces as separators by default, but can split on any
# character. GNU awk even lets you use a regex.
BEGIN { FS="." }
{ print $2 }
EOF
$ echo "A.b.c.d" | awk -f example6.awk
b
$ # You can assign columns, not just read them!
$ echo "1 2 3 4 5 6 7 8 9 10" | awk '{ $5="asdf" } 1'
1 2 3 4 asdf 6 7 8 9 10
$ # There are lots of special variables available. NF is the number of
$ # columns, starting at 1 ( which makes $NF the last column ).
$ # NR is the total number of lines.
$ # FNR is the line-number in the current file being read.
$ # You can tell it to read stdin with -. If no files are given, it also reads stdin.
$ # FILENAME is a special variable which tells you what file awk is currently reading.
$ printf "%s\n" a b c d e f g | awk '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk
File - Total 1 Current 1 a
File - Total 2 Current 2 b
File - Total 3 Current 3 c
File - Total 4 Current 4 d
File - Total 5 Current 5 e
File - Total 6 Current 6 f
File - Total 7 Current 7 g
File example1.awk Total 8 Current 1
File example1.awk Total 9 Current 2 # Comments begin with # like in shell
File example1.awk Total 10 Current 3
File example1.awk Total 11 Current 4 # It takes a list of expressions and code blocks, and for every line
File example1.awk Total 12 Current 5 # it reads, runs the code blocks in order.
File example1.awk Total 13 Current 6 { print "This runs first" }
File example1.awk Total 14 Current 7 { print "This runs second" }
File example1.awk Total 15 Current 8 { print "This runs third"; next }
File example1.awk Total 16 Current 9 { print "This would run fourth except for that next" }
$ # The OFS variable controls the output field separator. It defaults to a space.
$ # You can set variables outside the awk program with -v VAR=VALUE.
$ # So let's show what OFS does.
$ printf "%s\n" a b c d e f g | awk -v OFS="," '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk
File,-,Total,1,Current,1,a
File,-,Total,2,Current,2,b
File,-,Total,3,Current,3,c
File,-,Total,4,Current,4,d
File,-,Total,5,Current,5,e
File,-,Total,6,Current,6,f
File,-,Total,7,Current,7,g
File,example1.awk,Total,8,Current,1,
File,example1.awk,Total,9,Current,2,# Comments begin with # like in shell
File,example1.awk,Total,10,Current,3,
File,example1.awk,Total,11,Current,4,# It takes a list of expressions and code blocks, and for every line
File,example1.awk,Total,12,Current,5,# it reads, runs the code blocks in order.
File,example1.awk,Total,13,Current,6,{ print "This runs first" }
File,example1.awk,Total,14,Current,7,{ print "This runs second" }
File,example1.awk,Total,15,Current,8,{ print "This runs third"; next }
File,example1.awk,Total,16,Current,9,{ print "This would run fourth except for that next" }
$ # ORS is like OFS, except it controls what's printed at the end of a line. Defaults to newline.
$ printf "%s\n" a b c d e f g | awk -v ORS="," '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk
File - Total 1 Current 1 a,File - Total 2 Current 2 b,File - Total 3 Current 3 c,File - Total 4 Current 4 d,File - Total 5 Current 5 e,File - Total 6 Current 6 f,File - Total 7 Current 7 g,File example1.awk Total 8 Current 1 ,File example1.awk Total 9 Current 2 # Comments begin with # like in shell,File example1.awk Total 10 Current 3 ,File example1.awk Total 11 Current 4 # It takes a list of expressions and code blocks, and for every line,File example1.awk Total 12 Current 5 # it reads, runs the code blocks in order.,File example1.awk Total 13 Current 6 { print "This runs first" },File example1.awk Total 14 Current 7 { print "This runs second" },File example1.awk Total 15 Current 8 { print "This runs third"; next },File example1.awk Total 16 Current 9 { print "This would run fourth except for that next" },
$