1. 18IS33-UNIX and Shell Programming (Scheme and Solution) Page 8
Signature of the QuestionPaperSetter:
:
Signature of Scrutinizer
Subject Titles: UNIX and Shell Programming. Subject Code: 18IS33
Q.No. Solutions Marks
Allotted
8.a) THE BEGIN AND END SECTIONS:Awk statements are usully applied to all lines selected by
the address, and if there are no addresses, then they are applied to every line of input. But, if you
have to print something before processing the first line, for example, a heading, then the BEGIN
section can be used gainfully. Similarly, the end section useful in printing some totals after
processing is over.
The BEGIN and END sections are optional and take the form
BEGIN {action}
END {action}
6M
8.b) i) split(stg, arr, ch): it breaks up a string stg on the delimiter ch and stores the fields in an array
ii) substr (stg, m, n): it extracts a substring from a string stg. m represents the starting point of
extraction and n indicates the number of characters to be extracted.
iii) length: it determines the length of its arguments, and if no argument is present, the enire
line is assumed to be the argument.
iv) index(s1, s2): it determines the position of a string s2within a larger string s1.
8M
8.c) The Comparison Operators : awk provides the comparison operators like >, <, >=, <= ,==, !=.
~ and !~ : The Regular Expression Operators:
Variables and Expressions: Variables and expressions can be used with awk as used with any
programming language. Here, expression consists of strings, numbers and variables combined by
operators.
6M
9.a) String HandlingFunctionsin perl
length :-determines the length of its argument.
index(s1, s2) :-determines the position of a string s2 within string s1.
substr(str,m,n) :- extracts a substring from a string str, m represents the starting point of
extraction and n indicates the number of characters to be extracted.
uc(str) :- converts all the letters of str into uppercase.
ucfirst(str): converts first letter of all leading words into uppercase.
reverse(str):- reverses the characters contained in string str.
8M
9.b) #!usr/bin/perl
print "Perl Script to Print Leap years!nn";
print "Enter Start Year: ";
$startYear=<STDIN>;
print "Enter End Year: ";
$endYear=<STDIN>;
print "Leap years are:n";
# loop through between thestart and end year
for($i=$startYear; $i <= $endYear; $i++){
if( (0 == $i % 4) && (0 != $i % 100) || (0 == $i % 400) ){
print "${i}n";
}
6M