Effortlessly Sorting and Filtering Data with awk in Linux.

Effortlessly Sorting and Filtering Data with awk in Linux

Awk is a powerful text manipulation tool in Linux that is widely used for sorting and filtering data. The name ‚awk‘ is derived from the initials of its creators – Alfred Aho, Peter Weinberger, and Brian Kernighan. Awk has a simple syntax, and it reads the input file record by record, and applies the specified action to them. In this article, we’ll explore how we can sort and filter data using awk in Linux with some code examples.

Splitting a Record into Fields

The first step in filtering and sorting data using awk is to split the single record into fields. We can do this by setting the field separator (FS) variable as shown below:

awk 'BEGIN {FS=","} {print $1,$2}' data.csv

In this example, we’ve set FS as a comma ‚,‘ and printed the first two fields of the data.csv file.

Filtering Records Based on Condition

Awk provides us with the ability to filter records based on a condition using the pattern-action structure. The pattern specifies a condition, and the action specifies what to do with the records that match that condition.

awk 'BEGIN {FS=","} $3 > 10000 {print $1,$2}' data.csv

In this example, we’ve filtered the records where the third field is greater than 10000 and printed the first two fields of that record.

Sorting Data

We can sort the data using the built-in functions such as sort, but it can become slow for a large dataset. Awk enables us to sort the data efficiently using the sort function. Below is an example of how to sort the data based on the second field:

awk 'BEGIN {FS=","} {print $0}' data.csv | sort -t ',' -k 2

In this example, we’ve printed all the records using $0 and have used the sort command with field separator set as a comma ‚,‘ and sort key as the second field -k 2.


Awk is a powerful tool in Linux that can be used to effectively filter and sort data. We can split the records into fields, filter records based on conditions using the pattern-action structure, and sort the data efficiently. Using the examples mentioned above, we can start exploring more advanced ways of manipulating data with awk. With its powerful features and ease of use, awk is a great tool to add to our data manipulation arsenal.