Start to understand AWK the easy way

Many people are terrified by the AWK shell command, and using it to get the information they want as a string for use on perhaps a pi display device.

I will try here to demystify AWK at the simplest of levels by using it to re-pose the release string on Raspbian Buster. On your pi – run the command :

lsb_release -d

This will give you the following output :

Description: Raspbian GNU/Linux 10 (buster)

This isn’t necessarily what you want as a string to display somewhere, in fact what I want to see is Raspbian 10 (buster). Looking at the output of “lsb_release -d” you’ll see that its split into 5 distinct elements split by spaces as follows :

Description:
Raspbian
GNU/Linux
10
(buster)

AWK identifies each of these elements as follows

$1     Description:
$2     Raspbian
$3     GNU/Linux
$4     10
$5     (buster)

To get my desired output of Raspbian 10 (buster), I can see that I need the elements $2, $4 & $5. To effect that from the command line we pipe an additional awk command onto the original command to process the original output into new output, and the awk command is as follows :

lsb_release -d |awk {'print $2 " " $4 " " $5'}

The net result of this is that we are printing output of the three desired elements and adding spaces in-between them. The command line output becomes

Raspbian 10 (buster)

Hopefully this takes away some of the mystery of awk a little.

Posted in Raspberry Pi and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *