1 Perl Syntax: substitution s// and character replacement tr//

Slides:



Advertisements
Similar presentations
Perl & Regular Expressions (RegEx)
Advertisements

The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
Adv. UNIX:Perl/81 Advanced UNIX v Objectives of these slides: –introduce Perl (version ) –mostly based on Chapter 1, Learning Perl
1 Perl: arrays. 2 #!/usr/bin/perl -w # bind3.pl # # Here's an example that takes a unix path ($file) # and copies it to anothe variable ($filename) #
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Programming and Perl for Bioinformatics Part III.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Linux+ Guide to Linux Certification, Second Edition
8.1 Last time on: Pattern Matching. 8.2 Finding a sub string (match) somewhere: if ($line =~ m/he/)... remember to use slash( / ) and not back-slash Will.
More Regular Expressions. List/Scalar Context for m// Last week, we said that m// returns ‘true’ or ‘false’ in scalar context. (really, 1 or 0). In list.
UNIX Filters.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Regular Expression A regular expression is a template that either matches or doesn’t match a given string.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen.
Lecture 8 perl pattern matching features
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.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Linux+ Guide to Linux Certification, Third Edition
January 23, 2007Spring Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Bioinformatics 生物信息学理论和实践 唐继军
1 Perl Syntax: control structures Learning Perl, Schwartz.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Pattern Matching II. Greedy Matching When dealing with quantifiers, Perl’s pattern matcher is by default greedy. For example, –$_ = “Bob sat next to the.
– Introduction to Perl 12/12/ Introduction to Perl - Searching and Replacing Text Introduction to Perl Session 7 ·
Strings and Patterns in Perl Ellen Walker Bioinformatics Hiram College.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Finding substrings my $sequence = "gatgcaggctcgctagcggct"; #Does this string contain a startcodon? if ($sequence =~ m/atg/) { print "Yes"; } else { print.
The Scripting Programming Language
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Linux Administration Working with the BASH Shell.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Chapter 18 The HTML Tag
String and Lists Dr. José M. Reyes Álamo.
Introduction to Python
CSC 4630 Meeting 7 February 7, 2007.
Regular Expressions in Perl
Unix Talk #2 (sed).
String and Lists Dr. José M. Reyes Álamo.
Introduction to Python
Linux Shell Script Programming
Introduction to Python
Introduction to Computer Science
The Selection Structure
CIS 136 Building Mobile Apps
Regular Expression: Pattern Matching
Presentation transcript:

1 Perl Syntax: substitution s// and character replacement tr//

2 Substitution Pattern matching is useful for finding or indexing items, but to modify the data, substitution is required. Substitution searches a string for a PATTERN and, if found, replaces it with REPLACEMENT. $line =~ s/PATTERN/REPLACEMENT/; Returns a value equal to the number of times the pattern was found and replaced. $result = $line =~ s/PATTERN/REPLACEMENT/;

3 s/// $_ = "one two"; s/^([^ ]+) +([^ ]+)/$2 $1/; # $_ = "green scaly dinosaur"; s/(\w+) (\w+)/$2, $1/; # s/^/huge, /; # s/,.*een//;# s/green/red/; # s/\w+$/($`!)$&/; # s/\s+(!\W+)/$1 /; # s/huge/gigantic/g; # $_= "fred barney"; if(s/fred/george/) {#s/// returns true if sucessful print "replaced fred with george\n";

4 s/// $_ = "one two"; s/^([^ ]+) +([^ ]+)/$2 $1/; #swap first 2 words $_ = "green scaly dinosaur"; s/(\w+) (\w+)/$2, $1/; # "scaly, green dinosaur" s/^/huge, /; #huge, scaly, green dinosaur s/,.*een//;#huge dinosaur s/green/red/; #fails s/\w+$/($`!)$&/; # huge (huge !)dinosaur next line ^^ --match _, !, and ) s/\s+(!\W+)/$1 /; # huge (huge!) dinosaur -- replace '_!)' with !)_ s/huge/gigantic/; #gigantic (huge!) dinosaur $_= "fred barney"; if(s/fred/george/) {#s/// returns true if sucessful print "replaced fred with george\n";

5 Character Replacement A similar operation to substitution is character replacement. tr/CHARACTER SEARCH LIST/REPLACEMENT LIST/ -- note that this is not pattern matching, but character matching -- returns number of characters replaced $line =~ tr/a-z/A-Z/; $count_CG = $line =~ tr/CG/CG/; $line =~ tr/ACGT/TGCA/; $line =~ s/A/T/g; # be CAREFUL $line =~ s/C/G/g; # this turns your sequence into all A|C $line =~ s/G/C/g; $line =~ s/T/A/g;

6 Character Replacement Flags tr/SEARCH_LIST/REPLACEMENT_LIST /c -- complement the SEARCHLIST -- SEARCH_LIST is comprised of all characters NOT in SEARCH_LIST tr/ / /d -- delete found but unreplaced characters tr / / /s -- squash duplicate replaced characters -- sequences (or runs) of characters replaced, are squashed down to a single character

7 Character Replacement while($line = ) { $count_CG = $line =~ tr/CG/CG/; $count_AT = $line =~ tr/AT/AT/; } $total = $count_CG + $count_AT; $percent_CG = 100 * ($count_CG/$total); print “The sequence was $percent_CG CG-rich.\n”;

8 Pattern Matching Flags gm// s// (not tr// ) match globally, find all occurrences iignore case mmatch multiple lines as continuous string s treat string as single line.

9 Examples $_ " AtttcgAtggctaaaAtttgctt" s/A/a/g;#atttcgatggctaaaatttgctt s/^\s+//; #strip leading white space s/\s+$//; #strip trailing white space Binding operator $string = " opps"; $string =~ s/^\s+//; # "opps"

10 Upper/Lower Case \U -- everything that follows is upper case \L -- what follows is lower case \u -- single character upper \l -- single character lower $_ = "I saw Barney with Fred"; s/(fred|barney)/\U$1/gi; I saw BARNEY with FRED

11 #!/usr/bin/perl # newNaive.pl # # Here is an example that shows how the "match" # returns a "true" -- so that on the "IF" control structure, # execution precedes into the block # # The $` is a special variable that "remembers" all of the # string that was passed over by the pattern matching engine. # # Using the length() function, the position of the match is determined, # and printed # $_ = "CCCATGATG"; if(/ATG/) { print "Found sequence at position ".length($`)."\n"; }

12 ` #!/usr/bin/perl # newNaive2.pl # # Here is an example that shows how the "match" # returns a "true" -- so that on the "IF" control structure, # execution precedes into the block # # The $` is a special variable that "remembers" all of the # string that was passed over by the pattern matching engine. # # Using the length() function, the position of the match is determined, # and printed # $_ = "CCCATAATTTAGTTTT"; if(/ATG/) { print "Found Start codong $& at position ".length($`)."\n"; } elsif (m/TAG/) { print "found stop codon $& at position ".length($`)."\n"; print "There are ".scalar(length($&)+length($'))." nucleotides after the stop, including the stop codon\n"; } elsif (m/TAA/) { print "found stop codon $& at position ".length($`)."\n"; print "There are ".scalar(length($&)+length($'))." nucleotides after the stop, including the stop codon\n"; } elsif (m/TGA/) { print "found stop codon $& at position ".length($`)."\n"; print "There are ".scalar(length($&)+length($'))." nucleotides after the stop, including the stop codon\n"; } else { print "Start/stop codons not found in $_\n"; }

13 #!/usr/bin/perl # sub.pl # # Example where I match "with" and " " and one or more # word characters # Then I replace all of that "with word" with "against 'word'" # # The $1 corresponds to the first set of parentheses. # $_="He's out bowling with Fred tonight"; s/with (\w+)/against $1/; print "$_\n";

14 #!/usr/bin/perl -w # bind3.pl # # Here's an example that takes a unix path ($file) # and copies it to anothe variable ($filename) # Then, we search for one or more of any character {.+} # followed by a "/" character -- but we have to use the # escape metacharacter "\" so that we don't end the match {\/}. # Finally, we are looking for one or more non-white spaces {(\S+) # at the end -- to pull off the the last file name "FOUND" # $path = "/home/tabraun/test/bob/FOUND"; $filename = $path; $filename =~ s/.+\/(\S+)/$1/; print "$filename\n";

15 #!/usr/bin/perl # randomSeq.pl # # Don't get too uptight over this line -- it is just setting # a "seed" for the rand() fuction with a value that approximates # a random number. If you must know, it takes a prccess ID ($$), # shifts its bit left 15 times, then add the process ID to the shifted # value, then does an bit-wise XOR (^) with the current time(). # print "Enter length of sequence to generate:"; $length = ; srand(time() ^ ($$ + ($$ << 15)) ); while($length) { # stay in loop until have generated enough sequence $rand = int rand(4); # Interger number between (0-3) inclusive $rand =~ tr/0123/ACTG/; $length = $length-1; #decrease loop counter $seq = $seq. $rand; #keep the nucleotide I just created } # Since I am out of the loop, I must be done print "$seq\n";

16 End