Awk
lastmod 2026-02-06created 2014-10-1729 edits
A programming language that runs without compilation. The name comes from the initials of its three co-developers: A. V. Aho, P. J. Weinberger, and B. W. Kernighan. Based on the observation that complex languages such as C are in practice used mostly for simple tasks like organizing data or documents, it was simplified to make data processing more convenient. It scans the input file, reads it in units of a predefined record separator, and gives each field a regular name using the field separator. By default, the record separator is a return (new line) and the field separators are blank and tab (tap); the record, i.e. all fields, is named $0, the first field $1, the second $2, and so on. It works with a simple structure in which each record is read and a programmed action is performed according to the pattern in its fields, and it is used for data validation, conversion, output in various formats, building databases, and more.
Options
-
-F - Specifies the field separator.
- ex)
awk -F"." ' /txt/ { print "mv " $1 "." $2 " " $1 ".doc" } '
-
-v 변수=값 - Assigns a value to a variable before the script runs.
-
-f - Specifies a script file.
Structure
- Start (BEGIN): Stage that converts the input data into a form suitable for execution
- Execution (Routine): Stage that processes the well-organized (normalized) data in the actual routine
- End (END): Additional output of results after the data has been processed
Built-in variables
| System variable | Description |
| FILENAME | Current file name |
| FS | Input field separator (default: |
| NF | Number of fields in the current record |
| NR | Current record number |
| OFMT | Output format for numbers (default: |
| OFS | Output field separator (default: blank line) |
| ORS | Output record separator (default: newline) |
| RS | Input record separator (default: newline) |
| $0 | Input record |
| $n | Nth field of the input record |
| ARGC | Number of command-line arguments |
| ARGV | Array containing the command-line arguments |
| ENVIRON | Associative array holding the environment variables |
| FNR | Same as NR, except that it applies to the current file |
| RSTART | Start of the string that satisfies the given match operation |
| RLENGTH | Length of the string that satisfies the given match operation |
Operators
| Arithmetic operators | |
| Conditional operator | |
| Logical operators | |
| Pattern operators | |
| Comparison operators | |
| Increment/decrement operators | |
| Field reference | |
Control statements
-
break -
continue -
do { Routine } while (condition) -
exit -
for ( init ; condition ; re ) { Routine } -
if ( condition ) { Routine } else { Routine } -
while (condition) { Routine } -
return
Functions
String functions
| Function name | Description |
| | Replaces regular expression r with string s throughout the entire input string. |
| | Replaces regular expression r with string s1 in input string s2. |
| | Returns the position of s2 in s1. 0 if not found |
| | Returns the length of the argument. |
| | Returns the position of the part of string s that matches regular expression r. |
| | Splits the given string into an array by the separator (default: space). |
| | Same as gsub, but even if several strings match the regular expression, only the first one is replaced |
| | Returns the string from position m to the end of string s. |
| | Returns the string from the m-th to the n-th position of string s |
| | Converts uppercase letters to lowercase. |
| | Converts lowercase letters to uppercase. |
Math functions
| Function name | Description |
| | archtangent value |
| | cos value |
| | Power of e, the base of the natural logarithm |
| | Returns as an integer |
| | Logarithm |
| | Random number from 0 to 1 |
| | sin value |
| | Square root |
| | Random number with the argument (if omitted, a random number is generated from the time) |
I/O functions
| Function name | Description |
| | Closes the given file. |
| | Closes the given command pipe. |
| | Deletes the given array element. |
| | Reads the next record. |
| | Reads from a file. |
| | Takes the next record as input. |
| | Prints the arguments |
| | Prints the arguments according to the format. |
| | Used the same way as printf, but only returns the value without printing it. |
| | Runs an internal system command. |
How to join multiple lines of output
You can use the following tools:
- bash - Concatenate lines given by grep -A - Stack Overflow
- sed#How to join multiple lines of output
- awk#How to join multiple lines of output
- tr#How to join multiple lines of output
| TODO |
| Needs to be tested |
Example
GCC Include directory
A bash sample for viewing the list of GCC's INCLUDE directories is as follows.
$ echo | gcc -Wp,-v -x c - -fsyntax-only 2>&1 \
| awk 'BEGIN{FS="[ \t]";RS="[\r\n]";s="#include <...> search starts here:";e="End of search list.";i=0;}{if($0!=""){if($0==e){i=0;}if(i==1){print($0);}if($0==s){i=1;}}}'
For reference, the cleaned-up AWK syntax is as follows.
BEGIN {
FS = "[ \t]";
RS = "[\r\n]";
s = "#include <...> search starts here:";
e = "End of search list.";
i = 0;
}
{
if ($0 != "") {
if ($0 == e) {
i = 0;
}
if (i == 1) {
print($0);
}
if ($0 == s) {
i = 1;
}
}
}
END {
}
Column print
Text Processing Commands
|
See also
Favorite site
- Wikipedia (en) description of awk
- The GNU Awk User's Guide 1
- AWK usage
- Wikibooks: GNU Awk User's Guide/Functions
- IBM technical library: Introduction to the AWK language 2
- IBM technical library: UNIX tips and tricks for beginners, Part 3: Filters and regular expressions: Using awk on the command line 3
- KLDP description of AWK 4
- Detailed usage of AWK 5
- AWK & SED chunk_1 6
- Batch-renaming files (in a format such as
([a-z]+)-([0-9]+).([a-z]+)) - AWK shell programming 7
Tutorials
- [Recommended] AWK script guide - mug896 8 (archive)