AWK
- Appeared in:
- 1977
- Influenced by:
- Paradigm:
- Typing discipline:
- File extensions:
- .awk
- Versions and implementations (Collapse all | Expand all):
AWK (name derived from surnames of authors) is an interpreted scripting programming language designed for processing text data.
AWK program takes a stream of text data (from file or console) as input and processes it row by row. The program is a series of pattern-action rules, written as pattern {action}. A pattern is an expression, and action is a series of commands. When input stream is processed, each line of data is matched against each pattern of the program, and the actions of matching patterns are excuted. Two special patterns BEGIN and END make the corresponding actions to be executed before and after processing the input, respectively.
Main data type in AWK is string, though numbers can be processed as well. Main data structure is an associative array. AWK is known for massive suport of regular expressions.
Elements of syntax:
| Inline comments | # |
|---|---|
| Case-sensitivity | yes |
| Variable identifier regexp | [_a-zA-Z][_a-zA-Z0-9]* |
| Variable assignment | varname = value |
| Variable declaration | none |
| Variable declaration with assignment | none |
| Grouping expressions | ( ... ) |
| Block | { ... } |
| Physical (shallow) equality | a == b |
| Physical (shallow) inequality | a != b |
| Deep equality | a == b |
| Deep inequality | a != b |
| Comparison | < > <= >= |
| Function definition | function functionName(argname1, ..., argnameN) |
| Function call | functionName(arg1, ..., argN) |
| If - then | if (condition) trueBlock |
| For each value in a numeric range, 1 increment | for (i = first; i <= last; i++) loopBody |
| For each value in a numeric range, 1 decrement | for (i = last; i >= first; i--) loopBody |
Examples:
Hello, World!:
Example for versions gawk 3.1.6, mawk 1.3.3The printing is done with BEGIN pattern, i.e., before processing the input.
BEGIN { print "Hello, World!" }
Factorial:
Example for versions gawk 3.1.6, mawk 1.3.3This example uses iterative factorial definition. Individual statements within code block can be separated with semicolons (;) or new lines.
BEGIN {
f = 1
print "0! = " f
for (i=1; i<17; i++) {
f *= i
print i "! = " f
}
}
Fibonacci numbers:
Example for versions gawk 3.1.6, mawk 1.3.3This example uses iterative definition of Fibonacci numbers. fib is an associative array, and pr is a string.
BEGIN {
fib[1] = 1
fib[2] = 1
for (i=3; i<17; i++)
fib[i] = fib[i-1]+fib[i-2]
pr = ""
for (i=1; i<17; i++)
pr = pr fib[i] ", "
print pr "..."
}
Quadratic equation:
Example for versions gawk 3.1.6, mawk 1.3.3{ A = $1
B = $2
C = $3
if (A == 0)
print "Not a quadratic equation"
else
{ D = B*B-4*A*C
if (D == 0)
print "x = " (-B/2/A)
else if (D > 0)
{ print "x1 = " ((-B+sqrt(D))/2/A)
print "x2 = " ((-B-sqrt(D))/2/A)
}
else
{ print "x1 = (" (-B/2/A) "," (sqrt(-D)/2/A) ")"
print "x1 = (" (-B/2/A) "," (-sqrt(-D)/2/A) ")"
}
}
}
Comments
]]>blog comments powered by Disqus
]]>