Introduction to awk.

awk is programminglanguage generally used for text processing and formatting.

It providesprogramming language facility to help better text processing and generateformatted report from large file.

It has facility to specify pattern,command or actions on the command line or in a program file to process the textand generate the formatted report by manipulating the data available in data file.

How it works?Here are the steps awk does for manipulating the data.

Reads one line ofdata from input.

The input can be data file and one line of data file is read.

Applies the specified pattern action orcommand.

It applies the specified pattern action or command on the line.

Provides output.

It displays the outputof applied specified pattern action or command on the line to standard output.

Again it reads one line of data from input,applies the specified pattern action or commands and provides output.

It doesthe same for all lines of data file.

Syntax.

This is the syntax of awk.

awkoptions program file.

Here options are optional.

file is the text file whichawk will read line by line.

program includes pattern, action or command whichwill be enclosed within single quotes and will be applied on a line of textfile.

awk options.

These are some awk options.

-f file File name to read theprogram from file.

-F fs field separator for data fields in a line.

Example.

OK.

Let's see some example.

awk command datafile.

pattern is not used,so the command will be applied on each line of datafile.

command is print $0.

Print is used to print the text and $0 represents the entire line.

We'll see it later.

OK.

Let's see another example.

Awk pattern datafile.

action or command is not used, so pattern will be applied andit will display the lines of datafile which has pattern text 'Unix'.

OK.

Let'ssee example which has both pattern and command.

awk pattern command datafile.

So pattern and command both are available, so it will check the pattern in eachline and if pattern is found then command will be applied.

So here ifpattern text Unix is found in line of datafile then command will be applied.

OK.

Let's see a small demo of awk.

OK.

Let's see awk with pattern.

So here input line is 'This is Unix' andwe are taking input from pipe and the pattern text is 'Unix' So you can see the input line isdisplayed because the pattern text 'Unix' is found ininput line.

OK.

Let's take another pattern.

So nothing got displaybecause the pattern text 'Linux' is not found in input line.

OK.

Let's take command with awk.

So we can see that text Command isdisplayed.

So for the input linethis command print Command is applied.

OK.

Now we'll take input fromstandard input.

So the input line is 'This is Unix' So the input line is displayed because thepattern text is 'Unix' and that is available in input line.

Let's takeanother input.

Nothing got displayed because thepattern text 'Unix' is not available in this input line.

Let's take another one.

Got displayed because pattern is available.

OK.

Now we will see for the command.

So the command is applied on this inputline.

Because pattern is not available,so command will be applied on each line.

Let's see another input.

We can see commandis applied on this line also.

The same for this line also.

OK.

Now we'lltake input from input file.

I have already written input file devices.

Config This file has configuration of differentdevices.

Let's see this file.

Device ID, Operating system, Memory,Number of processor, Location.

Let's see awk with pattern.

So here pattern text is 'Unix' and theinput file is devices.

Config.

So we can see,all the lines which has pattern text 'Unix' is displayed.

OK.

Let's take another pattern.

So we can see,all the lines which has pattern text 'Banglore' is displayed.

This one, this one and this one.

OK.

Let's see awk with command.

So here command is print $0and input file is devices.

Config $0 represents entire line.

So it willdisplay all the lines of devices.

Config So we can see all the lines ofdevices.

Config is displayed.

OK.

Now we will have bothpattern and command with awk.

So here pattern text is 'Unix' and thecommand is print "Unix Machine :" $0 So it will apply this command, if pattern text 'Unix' is found in any line.

So we can see pattern text 'Unix' is found in this line.

So this command is applied on this line.

print "Unix Machine :'" $0$0 is entire line, this line.

So Unix machine and this line is displayed and the same is for this line and this line.

We can see, Newyork dev004and Unix01.

So this is the way, we canhave both pattern and command.

Result is displayed on standard output.

Input file is not changed.

Let's see this one.

Let's see input file.

So we can seeinput file is not changed.

If you want to have the output in a filethen we have to redirect the output to a file.

OK.

So here we are redirecting theoutput to file temp.

Let's see the file temp.

So we can see the result is available in the file.

This one and this one is same.

OK.

Let's see how awk is used in shellscript.

I have already written one shell script.

So we can see, in this way we can use awk in shell script.

Here pattern text is 'Unix' and input file is devices.

Conflig Let's see the input file and now we willrun the shell script.

So we can see all the lines which has pattern 'Unix'is displayed.

dev001, dev004 and Unix01.

Multiple action or commands.

We can usesemicolon to have multiple action or command.

OK.

Let's go to terminal.

OK.

Let's see awk with multiplecommands.

So you can see,we have two commands and we are using semicolon after first command.

So we can see for each line these two commands are applied.

Print Machine so Machine is displayed.

Print $0 so entire input line is displayed.

OK.

Let's say this one.

I want to have both the line on one lineitself.

So, here we have print machine and then $0but this is only one command.

So you can see,this one command is applied on each line.

OK.

Let's see another way.

So this is another way we can use forproviding multiple commands and here on each line we will have onecommand and we don't have to use semicolon.

So you can see these two commands are applied on eachline of devices.

Config.

OK.

Let's see how we can have pattern So here we can see,we have two patterns 'Unix' and 'Linux'.

So these two patterns will be searchedin the line of devices.

Config and all the lines which has pattern 'Unix' or'Linux' will be displayed.

So we can see, all the lines which haspattern 'Unix' or 'Linux' got displayed.

(Visited 22 times, 1 visits today)
adminTipsawk,command,commands,in,Linux
Introduction to awk.awk is programminglanguage generally used for text processing and formatting.It providesprogramming language facility to help better text processing and generateformatted report from large file.It has facility to specify pattern,command or actions on the command line or in a program file to process the textand generate the formatted...

Do you want to be notified about new DBA updates, releases, jobs and free tips? Join our email newsletter. It's fast and easy. You will be among the first to know about hot new DBA updates and stuff, it will help you enhance your DBA skills.
We take your privacy very seriously