AWK is a common command in Linux, there are GAWK, NAWK and OAWK, generally Linux distributions use GAWK.

AWK handles text in rows, and by default uses space or \t as a separator to group a line and fill the corresponding field with the specified variable. The command takes the form of

awk 'condition{execute statement} condition{execute statement}' filename

There are two input sources

Multiple conditions are independent of each other, and will match the conditions one by one, and execute the response execution statement if the condition is met

Built-in variables

Examples of using conditional and execution statements.

awk '{FS=":"} $3<100{print $1 "\t" $3}' /etc/passwd

The output is.

The first line takes effect

The first line does not take effect, to make the first line take effect you need to use the keyword BEGIN

awk 'BEGIN {FS=":"} $3<100{print $1 "\t" $3}' /etc/passwd

Multiple commands

awk 'BEGIN {FS=":"} {print $1; print $2}' /etc/passwd

Multiple commands can exist in command brackets, but they should be separated by a “;” sign, or naturally separated by a line break.

String matching

Actually AWK can be used instead of grep, for example, our normal grep: !

If you change it to awk, it would be

Then there’s no grep, what? grep -v works well? Try this one.

awk '! /root/ { print $1}' /etc/passwd

Field Filtering

The example just demonstrated can only filter one line, however, we can do much more, we can filter only the username: ```

awk 'BEGIN {FS=":"} $1 ~ /root/{print $1}' /etc/passwd


Environment variables

Environment variables can be used in AWK as the variable ENVIRON, either by exporting the environment variable or by passing the -v argument when calling awk

$ awk -v val=$x '{print $1, $2, $3, val, ENVIRON["ts"]}' OFS="\t" /etc/passwd


  1. AWK short tutorial
  2. SKIP grep, use AWK