This post is regarding one of very useful feature of Awk, which provide flexibility to provide another delimiter with particular column while printing them through Awk. i know it could be confusing in theory, so let’s see How Awk Split Works to provide secondary delimiter within particular column.


For this example we are using Ubuntu machine 16.04. Although this Awk feature doesn’t depend upon Machine version, instead it could depend on Awk version. So using below setup

root@jarvis:~# lsb_release -d
Description:    Ubuntu 16.04.4 LTS
root@jarvis:~# awk -V
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
Copyright (C) 1989, 1991-2015 Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see

Now to see how its work, ww will take an example like below.

Awk Split Exampes

root@jarvis:~# ip add show eth0| awk '/inet /{print $2}'

Here we try to see IP Address of eth0 ethernet, but it also show subnet mask value. Sure some will say that we should use ifconfig command for same. Although topic is not that, but let check that as well

root@jarvis:~# ifconfig eth0| awk '/inet /{print $2}'

Here in ifconfig as well, we are having addr: append with IP Address. So if we have some feature which could use another level of delimiter which make it possible to separate IP Address with not-required part. Let’s see it once more with Awk split

root@jarvis:~# ip add show eth0| awk '/inet /{split ($2,a,"/"); print a[1]}'

root@jarvis:~# ifconfig eth0| awk '/inet /{split($2,a,":");print a[2]}'

With both examples, we can see we can able to print only IP Address out of other junk stuff. Now its time for brief understanding about Awk split

In this command, we basically used one another feature of Awk, which try to match regex and print line contain it. Describe in yellow font below

ifconfig eth0| awk '/inet /{split($2,a,":");print a[2]}'

Actully Awk split, store second column in another variable named a and apply secondary deleimeter on it

ifconfig eth0| awk '/inet /{split($2,a,":");print a[2]}'

In second part of split it print column as per desired requirement (through 1 based indexing not 0 based)with help of secondary delimiter in stored variable.

ifconfig eth0| awk '/inet /{split($2,a,":");print a[2]}'

Another Example

let’s see another example where we are not using any other Awk feature except split. Below, we are trying to print fourth written in echo statement

root@jarvis:~$ echo "first second third fourth:fifth" | awk '{split ($4,a,":"); print a[1]}'