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
-F
With this flag you can specify FIELD SEPARATOR (FS), and thus don't need to use the BEGIN
-v
Can be used to specify variables(like we did in BEGIN{OFS=":"})
-D
You can debug your .awk scripts specifying this flag(awk -D script.awk)
-o
To specify the output file (if no name is given after the flag, the output is defaulted to awkprof.out)
f
Reads the AWK program source from the file program-file, instead of from the first command line argument
--dump-variables
Prints a sorted list of global variables and their final values to file
Built in Variables
ARGC
It implies the number of arguments provided at the command line
ARGV
It is an array that stores the command-line arguments. The array's valid index ranges from 0 to ARGC-1
CONVFMT
It represents the conversion format for numbers. Its default value is %.6g
ENVIRON
It is an associative array of environment variables
FILENAME
It represents the current file name
FS
It represents the (input) field separator and its default value is space
NF
It represents the number of fields in the current record
NR
It represents the number of the current record
FNR
It is similar to NR, but relative to the current file
-
OFMT
It represents the output format number and its default value is %.6g
OFS
It represents the output field separator and its default value is space
ORS
It represents the output record separator and its default value is newline
RLENGTH
It represents the length of the string matched by match function
RS
It represents (input) record separator and its default value is newline
RSTART
It represents the first position in the string matched by match function
SUBSEP
It represents the separator character for array subscripts and its default value is \034
$0
It represents the entire input record
$n
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
%c
single character
%d
decimal integer
%e
[-]d.dprecisionE[+-]dd
%f
[-]ddd.dprecision
%g
e or f conversion, whichever is shorter, with nonsignificant zeros suppressed
%o
unsigned octal number
%s
string
%x
unsigned hexadecimal number
%%
print a %; no argument is converted
printf examples
49
4.950000e+01
49.500000
49.50
49.5
61
000061
31
|January|
| January|
|January |
|Jan|
| Jan|
|Jan |
%
Examples
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
Was this helpful?