AWK Programming

INTRODUCTION:
AWK is very powerful tool that can be used with the UNIX Shell scripting. The AWK provides variety of capabilities that enables programmer to format the output from text files to the desired format. We will be discussing the different formatting option using AWK. The AWK programming is very similar to C programming. If you are familiar with the C programming, it will be very easy for you to learn AWK.

The AWK derives its name from its author Alfred Aho, Peter Weinberger, and Brian Kernighan. It is an interpreted programming language, used for text processing, formatting and reporting.

How does AWK works?
AWK reads the input text via file or other command output line by line. It performs action on each line based on the command specified under the AWK and treat each line as a record. Each line is further broken into different fields depending upon the field separator provided.


Built-in Variables in AWK:
Before we proceed with any of the AWK's command lets explore the built in variables in AWK:
$0Holds the entire record(or line)
$1,$2...Holds the first field, second field and so on
NRKeeps the total count of input record. If the number of lines input to AWK command is 10, the NR value equals 10
NFKeeps the total count of fields in a record.
FILENAMEContains the name of the input file.
FSThis variable contains the field separator for the AWK based on which each line is separated into different fields. The default field separator is white space which contains any number of space or tab characters. The field separator can be any character or combination of characters.
RSIt is used as record separator i.e. dividing each text file into different lines. The default record separator character is a "newline"
OFSIt stands for "output field separator", which separates the output of the fields printed by AWK. The default is a "space" character.
ORSIt stands for "output record separator", which separates the output of records printed by AWK. The default is a "newline" character.
OFMTIt provides the format specification that print uses with sprintf() when it wants to convert a number to a string for printing. The default format is "%.6g".

The print command:
The print command is used in AWK for printing the output. We will be discussing the usage of above mentioned built-in variables along with AWK.

Examples of using built-in variables in AWK:
We will be using the following file for showing the formatted output:


1. Using $0 variable: As you can see in the below output, the $0 variable prints the entire line as output.





Now we will print only those records for which the age is greater than 25.




2. Using $1, $2 etc:





Note if there is no comma provided between the $1 and $2, the output of the two fields are concatenated.

 3. Using NR variable:
 

4. Using NF Variable




The structure of the AWK Program:
In the above example we have observed the parts between the quote characters are programs written in AWK programming language. Each AWK program is a sequence of one or more pattern-action statements:
pattern {action}
pattern {action}
...
The AWK program scans each lines of input and searches for the match pattern. If the matched pattern condition is true, the corresponding action is executed.

How to run a AWK program?
In the preceding examples, we have seen one of the few ways of running AWK program. Let us explore all of them now in succeeding section:
1. awk 'program' <input files>:
The AWK reads through each lines of the file provided. We have seen many example to this style of calling AWK in preceding sections.

2. awk 'program':
In this example the AWK reads through the each console input, unless an EOF is encountered on the console. In UNIX system, the EOF is found on console by pressing ctrl+D on the console.


3. Writing the AWK program in a separate file and calling it with awk command:
The awk program enclosed within the single quotes can be written separately and called as follows:
awk -f <program file> <Optional Input Files>

CONTENT UNDER DEVELOPMENT...

No comments:

Post a Comment