Awk is a flexible pattern matching, text processing and line-oriented language, Linux provides us with an implementation version of it, gawk.

We can use the awk or gawk command to demonstrate. Awk is a symbolic link to gawk:

1
ll /bin/awk
img

OPTIONS

Here are some common options for gawk command:

Option Meaning
-f program-file or ‑‑file program-file Reads the gawk execution script from the file.
-F fs or ‑‑field-separator fs Specifies the value of the predefined variable FS. Use this value as the delimiter for the column.
-v var=val or ‑‑assign var=val Assign the value val to the variable var.

VARIABLES, RECORDS AND FIELDS

Here are some common built-in variables for gawk command:

Option Meaning
ARGC The number of command-line arguments.
ARGV Command line argument array.
FNR The number of records from the current input file.
FS The field separator for input content; the default is a space.
NF The number of fields from the input.
NR The number of records from the input.
OFS The field separator for output content; the default is a space.
ORS The record separator for output content; the default is a newline.
RS The record separator for input content; the default is a newline.

PATTERNS AND ACTIONS

Here are some common patterns and actions for gawk command:

PATTERNS

Option Meaning
BEGIN Print once at the beginning.
END Print once at the end.
/regular expression/ Executes a regular expression for each line of content.
relational expression Performs operator calculations on each line of content.
pattern && pattern or pattern || pattern or ! pattern Performs logical operations on each line of content.
pattern ? pattern : pattern Performs a ternary operation on each line of content.
pattern1, pattern2 Start with the pattern1-matched row until the pattern2-matched row.

ACTIONS

Option Meaning
{} Everything in parentheses are executable statements.

Operators

Option Meaning
$N Refers to the Nth field.
++ – Auto-increment and auto-decrement.
+ - ~ Plus, minus, and negation.
* / % Multiplication, division, and modulus.
space String concatenation.
< > <= >= == != Relational operators.
&& || Logical AND and OR.
?: Ternary operator. expr1 ? expr2 : expr3.
= += -= *= /= %= ^= Assignment.

I/O Statements

Option Meaning
print Outputs the current record, ending with the value defined by the built-in variable ORS.
printf Output formatted content.

The printf Statement

Option Meaning
%a, %A A hexadecimal floating point number.
%c A single character.
%d, %i The integer part of a decimal number.
%e, %E A floating point number.
%o An unsigned octal number.
%u An unsigned decimal number.
%s A character string.
%x, %X An unsigned hexadecimal number.

Examples

Run Program Only

Output Argument Count

With the ARGC variable, we can get the number of command-line arguments:

1
awk 'BEGIN{print ARGC}'    # Print the command line argument count
img

Output Argument Value

With the ARGV variable, we can get the value of the command-line argument:

1
awk 'BEGIN{print ARGV[0]}'    # Print the command line first argument value
img

Output Integer Variable

1
awk 'BEGIN{var=123;print var}'    # Print the self-defined integer variable
img

Output Integer Variables Summation

We can obtain the summation of two integer variables before the output:

1
awk 'BEGIN{var=123;var2=321;print var+var2}'    # Print the summation of these two integer variable
img

Output String Variable

1
awk 'BEGIN{var="abc";print var}'    # Print the self-defined string variable
img

Use the Input Stream as Content

Awk can also handle content from the input stream:

1
2
ll /
ll / | awk '{print $1}'
img

Handle File Content

As well as dealing with the content from the file.

Let’s prepare some test content first:

1
2
3
4
5
6
7
8
printf '%s\n' \
'user1:10020:10010:Dong Chen:/bin/bash:active' \
'user2:10021:10010:Dong Chen1:/bin/bash:inactive' \
'user3:10022:10010:Dong Chen2:/bin/bash:active' \
'user4:10023:10010:Dong Chen3:/bin/bash:inactive' \
'user5:10024:10010:Dong Chen4:/bin/bash:active' \
'user6:10025:10010:Dong Chen5:/bin/bash:inactive' > awk_file
cat awk_file
img

The BEGIN and END patterns will only print once:

1
awk -F: 'BEGIN{print "HEADER"}END{print "FOOTER"}{print $1,$2,$3,$4,$5}' awk_file
img

Output With A Customized Field Separator

Through -v argument, we can customize the output field separator’s variable OFS:

1
awk -v OFS=', ' -F: '{print $1, $4, $2, $3, $5}' awk_file
img

Output Formatted Content

Through printf command and its formats, we can print formatted content:

1
awk -F: '{printf "User Account: %s, Username: %s, User ID: %d, Group ID: %d, Login Shell: %s\n", $1, $4, $2, $3, $5}' awk_file
img

If Else Ternary Operation

We can use a ternary operator to do conditional statement operations:

1
awk -F: '($6=="active" ? status="Working hard" : status="Lazy"){printf "%s is %s.\n", $1, status }' awk_file
img

References The GNU Awk User’s Guide, GAWK(1)

Buy me a coffeeBuy me a coffee