Introduction to Unix – CS 21

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

● Perl reference
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
Practical Extraction & Report Language Picture taken from
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Guide To UNIX Using Linux Third Edition
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Practical Extraction & Report Language PERL Joseph Beltran.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Linux+ Guide to Linux Certification, Third Edition
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Introduction to Unix – CS 21 Lecture 8. Lecture Overview More detail on emacs and vi Regular expression matching in emacs and vi.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
LIN Unix Lecture 7 Hana Filip. LIN Text Processing Command Line Utility Programs (cont.) sed LAST WEEK wc sort tr uniq awk TODAY join paste.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Sed. Class Issues vSphere Issues – root only until lab 3.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Input from STDIN STDIN, standard input, comes from the keyboard.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Unix Talk #2 (sed).
Perl I/O Learning Objectives:
Introduction to Perl Learning Objectives:
Introduction to Bash Programming, part 3
Presentation transcript:

Introduction to Unix – CS 21 Lecture 15

Lecture Overview Perl Programming Why would you want to use it? How does it compare to awk, sed, and bash? Syntax Semantics

Why Perl? Perl tries to be a little bit of everything There’s More Than One Way To Do It TMTOWTDI Designed to perform string manipulation and regular expression matching Capability to perform all the same tasks that awk and sed perform with little effort

Perl Mentality Perl was designed to try to make common activities simple Also designed to make not-so-common activities not that complicated If you get good at perl, you can pack a lot of information into small programs Perception: You’re not a hacker unless you know perl

Running Perl One liners Perl scripts perl –e ‘print “Hello world!\n” ;’ Perl scripts #!/usr/bin/perl –w Adds warnings, which is VERY important

Major Differences From Bash and Awk programs Every line must end with a ; In bash and awk, simply hitting return ended a command Every use of a variable must have the $ In bash: myVar=0 In perl: $myVar=0 ;

Printing In Perl The print statement print $myVar ; print $myVar, “ and “, $myOtherVar ; Doesn’t go on to a new line like echo does in bash Need to add an explicit marker for the end of the line: “\n” print $myVar, “\n” ;

Variables In Perl Declared bash style (no previous declaration) Scalars A single value. (5, “Hello”, 4.3) $variableName Arrays A group of values @arrayName Others Automatic Interpretation

What Is An Array? “hi” $variable1 “there” $variable2 5.3 $variable3 4.1 $variable4 “hi” “there” 5.3 4.1 @array 1 2 3

Accessing Array Elements In Perl All elements are numbered starting from zero Accessing the array as a whole requires the @ Accessing each individual element requires the $ @myArray = (5, “Hello”, 4.3”) ; print $myArray[1], “\n” ;

File Input And Output In order to read or write to an external file, we need a file handle Special variable that refers to an external file Should be in all caps to avoid confusion Reading a line from a file: <FILE> Writing to a file: print FILE “hello file!\n” ;

Declaring File Handles: The Open Command open(HANDLE, “filename”) ; Open for reading open(HANDLE, “> filename”) ; Open for writing open(HANDLE, “>> filename”) ; Open for writing by appending

STDIN, STDOUT, And STDERR As usual, three files/streams are already ready to go whenever you run a perl program STDIN, STDOUT, and STDERR Read lines one at a time from STDIN $line = <STDIN>

Example

Cutting Off The Newline: chomp When reading in a line at a time, perl keeps the newline at the end of the line Bash doesn’t, and awk doesn’t Use the chomp command to get rid of it chomp($line = <STDIN>) ;

Chomp Example

Special Cases Made Easy Focusing on the idea that common cases should be made easy, there are a lot of shortcuts available in perl $line = <> ; Reads a line at a time from all of the files listed on the command line or STDIN if no files were specified Acts just like other Unix programs

Example Of <>

The Default Variable: $_ To make shortcuts even easier, if you don’t assign a value, the results are automatically stored in a default variable named $_ <STDIN> ; Stores the first line read into the default variable Other commands will use this default variable if no variable is supplied, making a lot of work go on “behind the scenes” Is this useful?

Example: Cat In Perl

Conditional Statements Just like awk and bash, perl has a set of statements that control the flow of execution if unless Conditions are based upon comparisons or tests

Comparison Operators Numbers Strings Description == eq Equals != ne Not equal > gt Greater than < lt Less than <= le Less than or equal >= ge Greater than or equal <=> cmp Compare (0=, 1>, -1<)

File Operations -e -d -f -T File exists File is a directory File is a normal file -T File is a text file

If statements if (CONDITION) { STATEMENTS ; }

Examples Of if

If..elsif..else statements if (CONDITION) { } elsif (CONDITION) else

Unless Statement unless (CONDITION) { STATEMENTS ; }

Unless Flowchart unless Is Condition True? no Statements yes

Unless Example

Another Form Perl also has a form that is more like English For single statements, you can place the if or unless and condition after the statement if (5>3) { $myVar = 4 ; } $myVar = 4 if (5 > 3) ;

Looping Statements To allow repetition, several looping statements are allowed just like bash and awk while for foreach

While Statements while (CONDITION) { STATEMENTS ; }

For Statements for (INIT ; CONDITION; INCREMENT) { STATEMENTS ; }

Example Of for

Foreach Statements foreach $var ( @array ) { STATEMENTS ; }

Example Of foreach

Regular Expressions Syntax is mostly that of egrep with a couple of differences Works on the default variable Default behavior is greedy Will match the largest string possible ? Restricts the match to the smallest possible

Example Jason Villarreal:11342:Midterm1:59:100 /:(.*):/ /:(.*?):/

Substitution In Perl Just like in vi s/old/new/ Works on the default variable In order to work with other variables, another operator is needed

Sed Functionality In Perl A special comparison operator: =~ Checks to see if a pattern appears in a variable Example: $line =~ /Jason/ ;

Example: Deleting All Quiz #3’s sed ‘/Quiz 3/d` database Perl: open(DATABASE, “database”) ; while ($line = <DATABASE>) { print $line if ! ($line =~ /Quiz 3/) ; }

Backreferences In R.E. Every time you use parenthesis in a regular expression, the pattern matched becomes marked and can be accessed later Marked by a number \1 stands for the first () \2 stands for the second () Etc…

Example Of Back References Matching HTML tags <LI> … </LI> <H1> … <H2> </H2> </H1>

Example

Awk Functionality In Perl: split Breaking up a line or string based on a delimiter is done with a call to split Usage: split( Delimiter, Record ) ; Delimiter can be a regular expression Examples: @fields = split(“:”, $record) ; ($field1, $field2) = split(“:”, $record) ;

Example: Printing Out The Third Field Awk: { print $3 } Perl: while ($line = <FILE>) { @fields = split(“:”, $line) ; print $fields[2] ; }

In Lab Today Practice with tar and perl Writing very small perl programs Rewriting some previous programs in perl

Next Week LaTeX Make