In AWK, there are two main functions for printing output: `print` and `printf`.
The `print` function is used to print values to the standard output, with each value separated by the output field separator (by default, a space character). Here is an example of using the `print` function to print two variables:
x = 10 y = 20 print x, y
This will output:
10 20
The `printf` function is similar to the C `printf()` function and allows you to format output in a specific way. The syntax of `printf` is:
printf(format, value1, value2, ...)
The `format` argument is a string that specifies the format of the output, and the `value1`, `value2`, etc. arguments are the values to be printed. The format string can include placeholders for the values, which are indicated by a percent sign (`%`) followed by a format specifier. Here is an example of using `printf` to print a formatted string with two variables:
x = 10 y = 3.14159 printf("x = %d, y = %.2f\n", x, y)
This will output:
x = 10, y = 3.14
In the `printf` format string, `%d` is a format specifier for an integer value, and `%.2f` is a format specifier for a floating-point value with two decimal places.
You can also use escape sequences in `printf` format strings to insert special characters, such as newline (`\n`) or tab (`\t`). Here is an example of using escape sequences in a `printf` format string:
printf("Name\tAge\n") printf("John\t%d\n", 25) printf("Jane\t%d\n", 30)
This will output:
Name Age John 25 Jane 30
In addition to `print` and `printf`, AWK also provides other output-related functions, such as `getline` for reading input from a file or command, and `sprintf` for formatting strings without printing them to the standard output. Refer to the AWK documentation for more information on these functions.