Wednesday, July 6, 2011

AWK

By : Wencesl Joseph & Priyanka L K(1025936)

Definition :awk is a powerful Unix command. It allows the user to manipulate files that are structured as columns of data and strings.

Summary

· Awk is a language used for processing files.

· Awk can be used for extracting the required fields from a file.

· $1, $2 etc. represent the contents of the file. They represent the columns of the current input line.

· $0 is used to print the contents of a file

· Actions performed by awk should be enclosed within { }.

· Awk is the only filter that uses whitespaces as the default delimiter.

· Contents of a file can be edited.

· This command is simple to use as its syntax is similar to that of C language

· Strings can be manipulated by using the various string manipulation functions.

· Awk can perform arithmetic computations.

EXAMPLES

Created files : f1, f2

Contents of f1

Name :Priyanka L K

Mail id : prilk.11@gmail.com

Name :Wencesl Joseph

Mail id : joeseph.drogba11@gmail.com

·          awk< f1 '{ print $4 }'
Output:      L
                   prilk.11@gmail.com
                   Joseph
                   joeseph.drogba11@gmail.com

· awk'{print $1,$3}' f1

Output :

Name Priyanka

Mail :

Name Wencesl

Mail :

· To turn a document with single-spacing into a document with double-spacing.

awk '{ print ; print ""}' f1 > f2

Output :

Name :Priyanka L K

Mail id : prilk.11@gmail.com

Name :Wencesl Joseph

Mail id : joeseph.drogba11@gmail.com

· To print the contents of file we use ‘$0’

awk '{print $0}' f1

Output :

Name :Priyanka L K

Mail id : prilk.11@gmail.com

Name :Wencesl Joseph

Mail id : joeseph.drogba11@gmail.com

· date | awk '{print $2 " " $6}'

Output : June 2011

To print only the second and sixth fields of the date command

Evaluating expressions using awk

· awk

> 'BEGIN { printf "%f\n", 76/8 }'

Output : 9.50000

· awk 'BEGIN { x=(((5*3)+7)/4)

>printf"%f\n",x}'

Output : 5.500000

Accepting strings & displaying Strings

· awk '

> BEGIN {

>printf "Enter your name: "

>getline name < "-"

>print name

> }'

Output:

Enter your name: Joseph

Joseph

Programs using awk

· To find if a no is odd or even

· awk 'BEGIN {

>printf "Enter value :\n "

>getline value < "-"

>if (( value % 2) == 0 )

>printf"Even"

>else

>printf"Odd"}'

Output :

Enter value

32

Even

· To find the length of string

· awk 'BEGIN {

>printf "Enter String : "

>getline string < "-"

>l = length(string)

>printf"%d\n",l}'

Output :

Enter String :priyanka

8

· Converting a string to lower & upper case

· awk 'BEGIN {

>printf "Enter string: "

>getline string < "-"

> s = tolower(string)

> s1=toupper(string)

>printf"%s \n%s\n",s,s1}'

Output : Enter string: WenCesl

wencesl

WENCESL

Using the ‘-f’ Option

· BEGIN

{

printf "Enter string: "

getline string < "-"

s = tolower(string)

s1=toupper(string)

printf"%s \n%s\n",s,s1

}

We can save the above code in a file say file1 and execute this command

· awk –ffile1

Output : Enter string: pRiYA

priya

PRIYA

Thus programs can be saved in a file and executed using the awk –f command

References :

ü http://www.grymoire.com/Unix/Awk.html

ü http://www.vectorsite.net/tsawk_3.html

ü http://kb.iu.edu/data/afja.html

ü http://www.cyberciti.biz/faq/bash-scripting-using-awk/

No comments:

Post a Comment