0% found this document useful (0 votes)
201 views

Programming With Awk and Perl

The document provides an overview of programming with awk and perl. It discusses: 1) The basics of awk including pattern matching, control statements, input/output statements, and numeric/string functions. Awk operates by applying commands to each line of a file. 2) Formatting, variables, math functions, conditional statements, and string operators in perl. Perl variables are called scalars and hold single values like numbers or text. Conditional statements include comparison operators. 3) Both awk and perl are scripting languages used for text processing and programming tasks. Awk operates on each line of a file while perl supports variables, functions, and other programming constructs.

Uploaded by

Ujjwal Pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views

Programming With Awk and Perl

The document provides an overview of programming with awk and perl. It discusses: 1) The basics of awk including pattern matching, control statements, input/output statements, and numeric/string functions. Awk operates by applying commands to each line of a file. 2) Formatting, variables, math functions, conditional statements, and string operators in perl. Perl variables are called scalars and hold single values like numbers or text. Conditional statements include comparison operators. 3) Both awk and perl are scripting languages used for text processing and programming tasks. Awk operates on each line of a file while perl supports variables, functions, and other programming constructs.

Uploaded by

Ujjwal Pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Topic Programming with awk and perl

Roll No-2514216

AWK
a) AWK Basics
An awk program operates on each line of an input file. It can have an optional BEGIN{} section of
commands that are done before processing any content of the file, then the main {} section works
on each line of the file, and finally there is an optional END{} section of actions that happen after
the file reading has finished
BEGIN { . initialization awk commands }
{ . awk commands for each line of the file}
END { . finalization awk commands }
b) AWK Pattern Matching
AWK patterns include regular expressions (uses same syntax as 'grep -E') and combinations using
the special symbols '&&' means 'logical AND', '||' means 'logical OR', '!' means 'logical NOT'. You
can also do relational patterns, groups of patterns, ranges, etc.
c) AWK control statements
if (condition) statement [ else statement ]
while (condition) statement
do statement while (condition)
for (expr1; expr2; expr3) statement
for (var in array) statement
break
continue
exit [ expression ]
d) AWK input/output statements include:
close(file [, how]) - Close file, pipe or co-process.
getline - Set $0 from next input record.
getline <file - Set $0 from next record of file.
getline var - Set var from next input record.
getline var <file - Set var from next record of file.
next - Stop processing the current input record. The next input record is read and processing
starts over with the first pattern in the AWK program. If the end of the input data is reached,
the END block(s),if any, are executed.
Nextfile - Stop processing the current input file. If the end of the input data is reached, the END
block(s), if any, are executed.print Prints the current record.
print expr-list Prints expressions.
e) AWK numeric functions include:
atan2(y, x) - Returns the arctangent of y/x in radians.
cos(expr) - Returns the cosine of expr, which is in radians.
exp(expr) - The exponential function.
int(expr) - Truncates to integer.
log(expr) - The natural logarithm function.
Rand() - Returns a random number N, between 0 and 1, such that 0 <= N < 1.
sin(expr) - Returns the sine of expr, which is in radians.
sqrt(expr) - The square root function.
srand([expr]) - Uses expr as a new seed for the random number generator.
f) AWK string functions include:
gsub(r, s [, t]) - For each substring matching the regular expression r in the string t, substitute
the string s, and return the number of substitutions. If t is not supplied, use $0.
index(s, t) - Returns the index of the string t in the string s, or 0 if t is not present.
length([s]) - Returns the length of the string s, or the length of $0 if s is not supplied.
match(s, r [, a]) - Returns the position in s where the regular expression r occurs.
split(s, a [, r]) - Splits the string s into the array a using the regular expression r, and returns the
number of fields. If r is omitted, FS is used instead.
sprintf(fmt, expr-list) - Prints expr-list according to fmt, and returns the resulting string.
strtonum(str) - Examines str, and returns its numeric value.
sub(r, s [, t]) - Just like gsub(), but only the first matching substring is replaced.
substr(s, i [, n]) - Returns the at most n-character substring of s starting at i.
tolower(str) - Returns a copy of the string str, with all the upper-case characters in str translated
to their corresponding lower-case counterparts. Non-alphabetic characters are left unchanged.
toupper(str) - Returns a copy of the string str, with all the lower-case characters in str translated
to their corresponding upper-case counterparts. Non-alphabetic characters are left unchanged

PERL
a) Formatting
Normal expository text is in this fontText that represents what you type is shown like so:
perl myprogram.pl
Things written in angle brackets should be replaced with the appropriate text:
perl <your program> -w

b) Perl scalar variables


Variables hold data. In Perl, the main variable type is called a scalar variable. A scalar holds one thing.
This thing could be a number, some text, or an entire genome. For example, a variable might be
named $x. When speaking aloud, we do not say "dollar x". We just call it "x".
#!/usr/bin/perl
# scalar.pl by _insert_your_name_here_
use warnings;

$x = 3;
print($x, "\n")
The addition of #!/usr/bin/perlto the script means that we no longer have to type # perl scalar.pl
Safer programming: use strict
#!/usr/bin/perl
# strict.pl by _insert_your_name_here_
use strict; use warnings;

$pi = 3.14;
print "pi = $pi\n";

$pi = 3.141593;
print "pi = $pi\n";

c) Maths functions
#!/usr/bin/perl
# math.pl
use strict; use warnings;

my $x = 3;
my $y = 2;
print "$x plus $y is ", $x + $y, "\n";
print "$x minus $y is ", $x - $y, "\n";
print "$x times $y is ", $x * $y, "\n";
print "$x divided by $y is ", $x / $y, "\n";
print "$x modulo $y is ", $x % $y, "\n";
print "$x to the power of $y is ", $x ** $y, "\n"
d) Conditional statements

Operator Meaning Example


== equal to if ($x == $y)
!= not equal to if ($x != $y)
> greater than if ($x > $y)
< less than if ($x < $y)
>= greater than or if ($x >= $y)
equal to
<= less than or if ($x <= $y)
equal to
<=> comparison if ($x <=> $y)

Whitespace
Indentation and white space improve readability. Consider the following legal but confusing code
which omits tabs and spaces (and even some semicolons)
if($x>$y){print"1\n";if($x<5){print"2\n"}}else{print"3\n"}
A program must be readable above all else. A program that works but is unreadable is difficult to improve
or maintain.
e) String operators
To concatenate operator which in Perl is represented by the dot (.) character. This operator allows you
to join two or more strings together
#!/usr/bin/perl
# strings.pl
use strict; use warnings;
my $s1 = "Hello";
my $s2 = "World\n";
my $s3 = $s1 . " " . $s2;
print $s3;

You might also like