Home

Search IconIcon to open search

awk

Smth like UNIX SQL. Yup, it’s a whole language.

# Examples

Print the third column of a space-separated file:

1
awk '{print $3}' tables.txt

Print the last column of each line in a file, using a comma as a field separator:

1
awk -F ',' '{print $NF}' tables.txt

Print lines that match a condition:

1
awk '($10 >= min_value && $10 <= max_value)' tables.txt

Print first, second and last arguments:

1
awk '{print $1,$2,$NF}' some_text.txt
# Flags
# Variables
# Other info

Records are read one at a time, and stored in the field variable $0. The record is split into fields which are stored in $1, $2, …, $NF.

Data input stored in fields is string, unless the entire field has numeric form and then the type is number and string.

So you can treat it like both:

1
2
echo 24 24E | mawk '{ print($1>100, $1>"100", $2>100, $2>"100") }'
0 1 1 1