Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Thursday, November 17, 2016 at 21:57


This is the first part of GNU awk command line examples the series in UbuntuBuzz for complete beginners. I planned to continue my previously GNU sed examples the series here, for GNU awk. In this first article, I provide the simplest and basical things about printing in awk. GNU awk is available built-in in almost every GNU/Linux system, including Ubuntu. I hope this article and the rest of the series help everyone a lot.
Subscribe to UbuntuBuzz Telegram Channel https://telegram.me/ubuntubuzz to get article updates directly.


About GNU awk


awk is a programming language name and also a program name, originated from UNIX system (dated back around 30 years ago), which its name is an abbreviation from Aho-Weinberger-Kernighan (read https://en.wikipedia.org/wiki/AWK). As a computer program, awk works to process and manipulate text, especially to filter and edit it in a simple but advanced way, using its own AWK language. GNU awk is a program compatible completely with AWK language and the UNIX awk program, originated from GNU operating system, by The GNU Project. The official page of GNU awk is https://www.gnu.org/software/gawk.


Text Sample


This is the text used as example in this article.  I saved this text in a file named students.txt. I will demonstrate how to use awk towards this text file.

awk '{ print }' students.txt
NAME    ID      OS      MARK
Parker  01      Darwin  19
Zorro   02      BSD     56
Kent    03      GNU     11
Wayne   04      UNIX    9
Robin   05      Plan9   17
Logan   06      VMS     98


1. Basic Printing


Command:
$ awk '{ print }' students.txt

Output:
master@master:/tmp> awk '{ print $3,"\t",$4 }' students.txt
OS       MARK
Darwin   19
BSD      56
GNU      11
UNIX     9
Plan9    17
VMS      98
master@master:/tmp> 



2. Print Particular Column


Command:
$ awk '{ print $1 }' students.txt


Output:
master@master:/tmp> awk '{ print $1 }' students.txt
NAME
Parker
Zorro
Kent
Wayne
Robin
Logan
master@master:/tmp>


3. Print More Than One Columns


Command:
$ awk '{ print $3,"\t",$4 }' students.txt


Output:
master@master:/tmp> awk '{ print $3,"\t",$4 }' students.txt
OS       MARK
Darwin   19
BSD      56
GNU      11
UNIX     9
Plan9    17
VMS      98
master@master:/tmp>



4. Print Columns Reordered


Command:
$ awk '{ print $4,"\t",$3,"\t",$2,"\t",$1 }'


Output:
master@master:/tmp> awk '{ print $4,"\t",$3,"\t",$2,"\t",$1 }' students.txt
MARK     OS      ID      NAME
19       OS/2    01      Parker
56       BSD     02      Zorro
11       GNU     03      Kent
9        UNIX    04      Wayne
17       Plan9   05      Robin
98       VMS     06      Logan
master@master:/tmp>


5. Print A Line


Command:
$ awk 'NR==1' students.txt
$ awk 'NR==4' students.txt


Output:

master@master:/tmp> awk 'NR==1' students.txt
NAME    ID      OS      MARK
master@master:/tmp> awk 'NR==4' students.txt
Kent    03      GNU     11
master@master:/tmp>


About GNU awk


awk is a computer language name and also a program name, originated from UNIX system (dated back about 30 years ago), consisted of Aho-Weinberger-Kernighan. As a computer program, awk works to manipulate text, especially to filter and edit it in a simple but advanced way, using its own awk language. The program awk from UNIX is proprietary, but its specification is free, and GNU awk is a program from GNU operating system created by The GNU Project as free software to replace UNIX awk completely. GNU awk contains no UNIX awk source code, because it's created from scratch.