AWK programs consist of a series of rules, where each rule specifies a pattern to match and an action to perform. The basic syntax of an AWK rule is as follows:
pattern { action }
The `pattern` specifies a regular expression or condition to match against each line of input, and the `action` specifies one or more commands to execute for each matching line. If the pattern is omitted, the action is applied to every line of input. If the action is omitted, the default action is to print the entire line.
Here is an example of an AWK program that prints all lines of a file that contain the word “hello”:
awk '/hello/ { print }' file.txt
In this program, the pattern is the regular expression `/hello/` which matches any line that contains the word “hello”, and the action is the `print` command which prints the matching line to the standard output.
In addition to patterns and actions, AWK also provides a number of built-in variables and functions that can be used in AWK programs. Some common built-in variables include:
– `$0`: The entire line of input
– `$1`, `$2`, `$3`, etc.: The first, second, third, etc. fields of the input line, separated by a delimiter (by default, whitespace)
– `NF`: The number of fields in the input line
– `NR`: The current record or line number
-`FILENAME`: The name of the current input file being processed
Some common built-in functions in AWK include:
– `gsub()`: Global substitution function that replaces all occurrences of a regular expression with a given string
– `split()`: Splits a string into an array of substrings based on a delimiter
– `length()`: Returns the length of a string or array
– `printf()`: Formats and prints output in a specified format
Here is an example AWK program that prints the second field of each line of a file, formatted as a percentage with one decimal place:
awk '{ printf "%.1f%%\n", $2 * 100 }' file.txt
In this program, the action is the `printf` function, which formats and prints the second field of each line multiplied by 100, followed by a percent sign and a newline. The `%.1f` format specifier specifies that the number should be printed with one decimal place.