awk
awk linux command cheatsheet by Thamizhiniyan C S
Introduction
Awk is a scripting language used for manipulating data and generating reports.The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators.
Syntax
Program Structure
BEGIN Block
BODY Block
END Block
Important Flags
Flags | Description |
---|---|
| With this flag you can specify FIELD SEPARATOR (FS), and thus don't need to use the BEGIN |
| Can be used to specify variables(like we did in BEGIN{OFS=":"}) |
| You can debug your .awk scripts specifying this flag(awk -D script.awk) |
| To specify the output file (if no name is given after the flag, the output is defaulted to awkprof.out) |
| Reads the AWK program source from the file program-file, instead of from the first command line argument |
| Prints a sorted list of global variables and their final values to file |
Built in Variables
Variable | Description | Example |
---|---|---|
| It implies the number of arguments provided at the command line | |
| It is an array that stores the command-line arguments. The array's valid index ranges from 0 to ARGC-1 | |
| It represents the conversion format for numbers. Its default value is %.6g | |
| It is an associative array of environment variables | |
| It represents the current file name | |
| It represents the (input) field separator and its default value is space | |
| It represents the number of fields in the current record | |
| It represents the number of the current record | |
| It is similar to NR, but relative to the current file | - |
| It represents the output format number and its default value is %.6g | |
| It represents the output field separator and its default value is space | |
| It represents the output record separator and its default value is newline | |
| It represents the length of the string matched by match function | |
| It represents (input) record separator and its default value is newline | |
| It represents the first position in the string matched by match function | |
| It represents the separator character for array subscripts and its default value is \034 | |
| It represents the entire input record | |
| It represents the nth field in the current record where the fields are separated by FS |
printf statement
awk's printf statement is the same as that in C, except that the *
format specifier is not supported.
SYNTAX: printf
format
,
expr1
,
expr2
, ...,
exprn
awk printf conversion characters
Character | Prints expression as |
---|---|
| single character |
| decimal integer |
| [-]d.dprecisionE[+-]dd |
| [-]ddd.dprecision |
| e or f conversion, whichever is shorter, with nonsignificant zeros suppressed |
| unsigned octal number |
| string |
| unsigned hexadecimal number |
| print a %; no argument is converted |
printf examples
Command | Output |
---|---|
49 | |
4.950000e+01 | |
49.500000 | |
49.50 | |
49.5 | |
61 | |
000061 | |
31 | |
|January| | |
| January| | |
|January | | |
|Jan| | |
| Jan| | |
|Jan | | |
% |
Examples
Command | Description |
---|---|
To simply print a file | |
To search for a pattern inside a file | |
To list the words that are at 1st and 3rd fields | |
To number the lines | |
Split based on the character 'o' | |
Split with the character 'o' and print the total number of characters | |
Separate rows base with 'o' | |
To specify field delimeter while outputing | |
To specify record delimeter while outputing | |
Checks if fields 2, 3, and 4 are all empty. If they are, it prints a message indicating that not all scores are available for the item identified in the first field | |
Checks if fields 2, 3, and 4 are all empty. If they are, it prints a message indicating that not all scores are available for the item identified in the first field | |
Evaluates student scores and assigns a grade of "Pass" or "Fail" based on whether any of their scores are below 50. It then prints the student identifier along with their grade | |
To identify the performance grade for each student. If the average of the three scores is 80 or more, the grade is 'A'. If the average is 60 or above, but less than 80, the grade is 'B'. If the average is 50 or above, but less than 60, the grade is 'C'. Otherwise the grade is 'FAIL'. | |
Formats input data by printing each line as is, but inserts a newline character every two lines and a semicolon after every odd-numbered line. This formatting creates a specific structure in the output |
Last updated