Show Menu
Cheatography

AWK - Part 1 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

AWK - Origin

The name AWK comes from the last initials of its creators Alfred V. Aho, Peter J. Weinbe­rger, and Brian W. Kernighan.

Where AWK is available ?

AWK is preins­talled in most UNIX variants. Just we need to type
awk
in the command line.
On Windows we need to install "­Cyg­win­" or "­Uni­xUt­ils­"to use AWK

Why we use AWK ?

AWK is a domain­-sp­ecific language designed for text processing and typically used as a data extraction and reporting tool. It is used for manipu­lating text files which are devided into records or lines, and each line is devided into fields.
It doesn't require that every line have the same format. But if a file is more structured like spread­sheet or a database, then we can do many things with it.

AWK Operations

Find intere­sting records in a data file.
Print or save specific fields.
Perform manipu­lations like swapping the order of fields or combining them into one field.
Sophis­ticated data operations like join.

AWK Versions

Name
Release Year
Original awk
1978
One True Awk or nawk or BWK awk
1985
GNU awk or gawk
1990
 

AWK "­Hello World" Example

awk 'BEGIN {print "­Hello world"}' 
>>> Hello world

Print specific fields from a text file

Soppose a file "­nam­es.t­xt­" containing the following lines :
Albert Einstein
Barak Obama
Steve Jobs
Each line is composed of two fields which are firstname and lastname. We want to print these fields in the opposite order : Lastname followed by Firstname.
awk '{print $2, $1} names.txt' 
>>> Einstein Albert
>>> Obama Barak
>>> Jobs Steve
Suppose
N = number of fields per line

We get each field by
"­$n"
where
n in [1 .. N] 
So, what if we rewrite the previous command without a comma ? Let's see an example.
awk '{print $2 $1} names.txt' 
>>> Einste­inA­lbert
>>> ObamaBarak
>>> JobsSteve
What we got is a concat­enation of the two fields.

Print a comma between two fields (conca­ten­ation)

Let take names.txt as a file example and print a comma between the firstname and the lastname.
awk print{$1 "­," $2} 
>>> Albert­,Ei­nstein
>>> Barak,­Obama
>>> Steve,Jobs
 

Print full lines from a file

Let take the previous text file names.txt as an example:
awk '{print $0}' names.txt 
>>> Albert Einstein
>>> Barak Obama
>>> Steve Jobs
Or we can also simply write the following command:
awk '{print}' names.txt 
>>> Albert Einstein
>>> Barak Obama
>>> Steve Jobs

Print each line with its number of fields

Let take the following file awk.txt:
AWK is awesome
Let learn AWK together
AWK is used for text processing
We use NF to count and print the number of fields of each line.
awk '{print NF , $0}' awk.txt 
>>> 3 AWK is awesome
>>> 4 Let learn AWK together
>>> 6 AWK is used for text processing

Specify the number of field

We want to print Lines that contain exactly 6 fields in awk.txt, so we can use one of the following two commands:
awk NF == 6 '{print $0}' 
>>> AWK is used for text processing
awk 'NF ==6'
>>> AWK is used for text processing