DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH Name Designation Branch Institute Year/Semester Subject Subject Code Topic Duration Sub Topic Teaching Aids
: A V N L Sarojini : Lecturer : Computer Engineering : A.A.N.M. & V.V.R.S.R. Poly., Gudlavalleru. : III Semester : UNIX & C : CM-304 : Shell Programming & Filtering Techniques : 50 Min : Basic features of awk : PPT CM304.25
1
Objective On completion of this period you would be able to know Introduction to awk filter Example commands using awk Advantages of awk
CM304.25
2
Recap egrep and fgrep over comes the limitations of grep the egrep command handles multiple regular expressions the fgrep uses multiple fixed strings.
CM304.25
3
awk command awk is a report writing tool. awk is named after its inventors
“Aho”,
“Weinberger” and “Kernighan”.
awk is the most powerful tool for text manipulation.
CM304.25
4
awk Command It combines the features of several filters. It also accepts regular matching.
expressions
for
pattern
It
has ‘C’ language type programming constructs, variables, several built-in functions and so on.
CM304.25
5
Features of awk We can format the output of awk filter by using printf function. It accepts pattern as well as regular expression. The operators and syntax language. So it is easy to use.
CM304.25
resembles
‘C’
6
Features of awk It accepts multiple files as input. It allows to write its output into a file or we can send it as input to other command.
awk filter is a horizontal and vertical filter.
CM304.25
7
Features of awk awk allows declaring of variables. awk comes with several built-in functions that can operate on data. Example: sqrt()
CM304.25
8
awk command The general syntax of the awk command is $awk
‘line specifier{action}’
line specifier: It is the pattern (or) expression for which each record is searched in the input file. action: Action to be taken on those records that satisfy the line specifier. file(s): Input files for awk command. CM304.25
9
awk command Example: $awk `/sales/{print}’ emp.lst If the line specifier is missing then the action will apply to all the lines of the file. If the action is missing then the entire line will be printed. We can also use $0 to indicate explicitly. Example: $awk `/sales/’ emp.lst $awk `/sales/{print $0}’ emp.lst CM304.25
(or)
10
awk command awk identifies fields by special variables, $1, $2, $3 , - - which stands for first, second and third fields.
awk uses tab spaces as the default delimiter.
CM304.25
11
awk command $cat >emp1.lst 2567 | karthick | director | sales | 15000 2576 | Divya | manager| sales | 15000 3587 | pavan | director | production | 50000
$awk -F”|” `/sales/{print $2,$5}’ emp1.lst Karthick Divya
15000 15000
CM304.25
12
awk command For pattern matching awk uses regular expressions of egrep type. $awk -F”|” `/sa[kx]s*ena/{print $2,$3}’ emp.lst
CM304.25
13
awk command awk also accepts line address to select lines. We can do it by using built-in variable NR. $awk-F”|” ‘NR==3,NR==6/sales/{print $NR,$2,$3}’ emp.lst
CM304.25
14
awk command awk uses the ‘C’ like function called printf to format output. With printf we can control the column width and alignment. Example: $awk -F”|” >”printf %3d %-12s %d”\n{printf $NR,$2,$3}’ >emp.lst CM304.25
15
awk command Using logical and relational operators. $awk -F”|”’$2=“ramu”&&$3>5000{print $2,$3}’ emp.lst $awk -F”|”’$4==“director”||$4=“manager{print $2,$3,$4} emp.lst
CM304.25
16
awk command Using variables $awk -F”|” `/sales/{ >abc=abc+1 >”printf %3d %12s %d” $abc $2 $3}’ emp.lst
CM304.25
17
Summary awk is a report writing tool. awk is the most powerful tool for text manipulation. It combines the features of several filters.It also accepts regular expressions for pattern matching.
It has ‘C’ language type programming constructs, variables, several built-in functions and so on.
CM304.25
18
Quiz 1.awk command accepts pattern as well as regular expressions. [TRUE/FALSE]
CM304.25
19
Quiz 1.awk command accepts pattern as well as regular expressions. [TRUE/FALSE]
CM304.25
20
Quiz 2.awk uses pipe(|) as the default delimiter. [TRUE/FALSE]
CM304.25
21
Quiz 2.awk uses pipe(|) as the default delimiter. [TRUE/FALSE]
CM304.25
22
Quiz 3. NR is a built in function used by awk to format output. [TRUE/FALSE]
CM304.25
23
Quiz 3. NR is a built in function used by awk to format output. [TRUE/FALSE]
CM304.25
24
Frequently Asked Questions 1. Explain briefly about ‘awk’ command and its various options.
CM304.25
25