What is AWK, an introduction

August 2nd, 2010 47 views Leave a comment Go to comments

AWK is nothing but a simple and powerful UNIX filter command. You can use it to filter & format contents of a text file or to modify sometimes. For example;
count number of entries month wise from below file

	 10-Jul-10
	 23-Jul-10
	:
	 31-Jul-10
	 1-Aug-10
	:
	 4-Aug-10
	 5-Aug-10

Syntax idea:
Basic

awk 'NR==52 {print;exit}'

Complex

awk 'BEGIN {
		FS="^";OFS="^";
	}
	{
		for (i=1; i<=NF; i++) {
			gsub(/^[ t]+|[ t]+$/,"",$i);
		} print
	}'
	filename > file_name_new

You can call AWK as a programming language as well. Because, like other programming languages, it has control and conditional statements. It also let you create functions.

Instead of typing big syntax on command prompt, you can write AWK command in some file.

There are 3 versions of AWK:

AWK - the original from AT&T
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version

I love to work in basic version only. Because it is having limited functions, some restriction over regular expression and lesser features than NAWK, GAWK. And you can explore your intelligence with this. Moreover, If you write a program in AWK then it’ll run in NAWK and GAWK as well.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

  1. No comments yet.