Vous êtes sur la page 1sur 35

ASSIGNMENT ON SHELL SCRIPTING

(1) To remove all .txt files in a particular directory using for loop
#!/bin/bash echo "Enter directory name" read dir ls $dir for file in $( find $dir -type f -name "*.txt") do rm -f $file done

(2) To add, subtract, multiply and divide two numbers, use case
#!/bin/bash #making of a simple calculator(-+*/) echo "Enter A: addition M: multiplication S: subtraction D: division " echo "Now enter your option" read optn echo "Enter the two digits" read x read y case $optn in A) echo "$x+$y"|bc -l ;;

M) echo "$x*$y"|bc -l ;; S) echo "$x-$y"|bc -l ;; D) echo "$x/$y"|bc -l ;; *) echo "wrong input" ;; esac

Output: 1) sushmita@ltspbinfo1 ~]$ sh Ans2.sh Enter A: addition M: multiplication S: subtraction D: division now enter your option A enter the two digits 10 23 33 2) [sushmita@ltspbinfo1 ~]$ sh simple_calculator_using_case.sh Enter A: addition M: multiplication S: substraction D: division Now enter your option S Enter the two digits 23

10

13

(3) Find the average of n numbers:


#!/bin/bash Sum=0 echo "Enter the no" read n for (( i = 1 ; i <= n ; i++ )) do Sum=`echo "$Sum+$i"|bc` done avg=`echo "$Sum/$n"|bc -l` echo "The average is $avg"

Output: [sushmita@ltspbinfo1 ~]$ sh Ans3.sh Enter the no 5 The average is 3.0000000000000000000

(4) Reversing a string


#!/bin/bash

echo " Enter the string: " read str reverse = `echo "$str"|rev ` echo " \n $reverse" Output: [sushmita@ltspbinfo1 ~]$ sh Ans4.sh Enter the string: SUSHMITA ATIMHSUS

(5) Print the newtonian triangle of n numbers


#!/bin/bash echo "enter height" read h for ((i = 1; i <= h; i++)) do for (( j = i; j < h; j++ )) do echo -n " " done for (( j = 1; j <= 2 * $i - 1; j++ )) do echo -n "*" done echo

done for(( i = h - 1 ; i > 0 ; i-- )) do Output: [sushmita@ltspbinfo1 ~]$ sh Ans5.sh enter height 4 * *** ***** ******* ***** *** *

ASSIGNMENT ON PERL PROGRAMING

(1) Store your name in a variable. Have it print out your name as part of a hello statement sent to the screen. Try breaking your name into separate first name and last name variable.
#!/usr/bin/perl -w print "Enter your name"; chomp($name=<STDIN>); print "Hello $name\n"; my@nam=split(' ', $name); print "your 1st name: $nam[0]\n and your last name: $nam[1]\n"; exit; Output:

[sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q1.pl Enter your name Sushmita Basu Hello Sushmita Basu your 1st name: Sushmita and your last name: Basu

(2) Simple calculator


#!/usr/bin/perl #simple calculator print "Put the 1st integer\n"; chomp ($x=<STDIN>);

print "choose your option,\n in between + , - , / or *\n"; chomp ($o=<STDIN>); print "Now put the second integer\n"; chomp ($y=<STDIN>); if ($o eq "+") { $A=$x+$y; print "$x+$y=$A"; } elsif ($o eq "-") { $S=$x-$y; print "$x-$y=$S"; } elsif ($o eq "*") { $M=$x*$y; print "$x*$y=$M"; } Output: [sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q2.pl Put the 1st integer 2

Choose your option,

in between + , - , / or * + Now put the second integer 3 2+3=5

(3) Find the longest and shortest word in a text file


#!/usr/bin/perl -w print "Enter the name of your text file\n\n"; chomp ($file=<STDIN>); open (RD,"$file")||die ("error"); my$input=join(' ',<RD>); my$longestword=''; my$longestwordlength=0; foreach my$word(split(/\s+/,$input)) { if(length($word)>$longestwordlength) { $longestword=$word; $longestwordlength=length($word); } } my$shortestwordlength=length($longestword); foreach my$word(split(/\s+/,$input))

{ if(length($word)<$shortestwordlength) { $shortestword=$word; $shortestwordlength=length($word); } print "The longest word is:\n $longestword\n the shortest word is:\n $shortestword\n"; close(RD); exit;

(4) Compute the average word length in any supplied text file
#!/usr/bin/perl -w print "Enter the name of your text file:\n"; chomp($fil=<STDIN>); open(RD, "$fil")||die("error"); my$input=join(' ',<RD>); my$length=''; my$add=0; my$i=1; my$avg=''; foreach my$word(split(/\s+/,$input)) { $length=length($word);

$add= $add + $length; $i++ } $avg=$add/$i; print "$avg"; close(RD); exit; Output: [sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q4.pl Enter the name of your text file: gene.txt 3.8

(5) Determine the answer to the following equation: X=((a+b)2)/c


#!/usr/bin/perl print "Enter the value of A:"; $A=<STDIN>; print "Enter the value of B:"; $B=<STDIN>; print "Enter the value of C:"; $C=<STDIN>; $X=(($A+$B)**2)/$C; print "The value of X is $X\n:"; Output: [sushmita@ltspbinfo1 ~]$ perl asgn5_new.pl

Enter the value of A: 3 Enter the value of B: 4 Enter the value of C: 5 The value of X is 9.8

(6) Write a subroutine to sort a list of strings in increasing order of their length and demonstrate its utility.
#!/usr/bin/perl -w #subroutine to sort a list of srings increasing order of their length print"Enter your words:\n"; chomp(@words = <STDIN>);#removes newline character print "\nHere is your sorting words:"; foreach $word(sort mysort @words) { print "\n$word\n"; } #subroutine for sorting words sub mysort { length($a) <=> length($b); #sort by their length } exit; Output:
Enter your words: aeroplane bag cold

cat round Here is your sorting words: bag cat cold round aeroplane

(7) Prepare telephone directory. The script should have provisions for adding and deleting telephone numbers.
#!/usr/bin/perl -w #creating a phonebook my %phonebook; while (1) { my $choice = menu(); if ($choice == 1) { addPB(); } elsif ($choice == 2) { deletePB(); } elsif ($choice == 3) { showPB(); } elsif ($choice == 4) { exit(); } else { print "Invaild Entry Try again"; } } #creating the menu bar sub menu { print"1. Add an entry.\n"; print"2. Delete an entry.\n"; print"3. List all entries.\n"; print"4. Quit.\n"; print"Enter your choice (1-4): ";

chomp(my$prompt = <STDIN>); return $prompt; } #subroutine for adding name and phone number sub addPB{ print"Enter a name: \n"; chomp(my$name=<STDIN>); if (exists $phonebook{$name}){ print"Entry already exists\n"; } else { print"Enter phone: \n"; chomp(my$phone=<STDIN>); $phonebook{$name} = $phone; } } #subroutine for deleting entry sub deletePB{ print"Enter name: \n"; chomp(my$delname=<STDIN>); if (exists $phonebook{$delname}){ delete($phonebook{$delname}); print"The name and phone have been deleted\n"; } else { print"There is no such name in the phone book\n"; } } #show all entry sub showPB{ while (($key, $value) = each(%phonebook)){ print"NAME:$key, PHONE NO:$value\n"; } }

(8) Print any lines containing both of word gene & nucleotide somewhere.
#!/usr/bin/perl -w print "Enter your filename you want to search:"; $fname =<STDIN>; open(READ, "$fname")||die("error"); while($line=<READ>)

{ if($line=~ m/gene/g and $line =~ m/nucleotide/g) { print $line; } } close(READ); exit; Output: [sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q8.pl Enter your filename you want to search: gene.txt here is the gene , here is the nucleotide.

(9) To determine the reverse complement of a given nucleotide sequence specified by the user. (do not use reverse function)
#!/usr/bin/perl -w print "Enter your Sequence which you want to get reverse complement\n"; chomp ($sequence=<STDIN>); $sequ=$sequence; $sequ=~ tr/ATGCatgc/TACGtacg/; print "\n\n$sequence \n\nreverse complement is \n\n$sequ\n\n"; exit; Output: [sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q9.pl

Enter your Sequence which you want to get reverse complement ATGCAGTCAATGGGCATATA ATGCAGTCAATGGGCATATA reverse complement is TACGTCAGTTACCCGTATAT

(10) Determine the nucleotide and gc content of given sequence. Ask the user for input.
#!/usr/bin/perl -w #determine the GC content in a given sequence print "enter your sequence"; chomp($seq=<STDIN>); $tot= ($seq=~ tr/ATGCatgc//); $gc= ($seq=~ tr/GCgc//); $gc_percent= ($gc/$tot)*100; print "\n\n$seq : this nucleotide's GC content is $gc_percent%\n\n"; exit; Output: [sushmita@ltspbinfo1 ~]$ perl perl_assignment_2_Q10.pl enter your sequence ATGCCGTACGGAATGCACACAGTATGC ATGCCGTACGGAATGCACACAGTATGC : this nucleotide's GC content is 51.8518518518518%

(11) Read in a genebank nucleotide / protein file and print the only sequence as a continuous string.

#!/usr/bin/perl w Print enter the filename; $filename=<STDIN>; Print enter the output file; $fout=<STDIN>; Open(RH,<$filename)||diefile not found; Open(WH,>$fout); While($line=<RH>) { Chomp($line); $flg=0; $flg=o if($line =~ /\/\//); Print WH $line if($flg); $flg=1 if ($line =~/ /^ORIGIN/); if($line =~s/\s//) { Print WH $line; } if($line =~ s/\d//) {print WH $line; } } Close(RH);

(12) Read in a genebank nucleotide / protein sequence file and write a fasta formatted file for the same. Read sequences stored in a file and write output to file.
#!/usr/bin/perl -w print "Enter your genbank filename:\n"; chomp($fn=<STDIN>); print "Enter your output filename:\n"; chomp($op=<STDIN>); open(RR,"$fn")||die("error"); open(WW,">$op")||die("error"); while($line=<RR>) { if($line=~ /^\s+[0-9]/g) { $line=~ s/[\s+0-9]//g; print WW $line; } } close(RR); close(WW); exit;

(13) Read in a genbank nucleotide/protein file and write the 6 reading frames onto a file.
#!/usr/bin/perl -w

use strict; use warnings; use BeginPerlBioinfo; # Initialize variables my @file_data = ( ); my $dna = ''; my $revcom = ''; my $protein = ''; # Read in the contents of the file "sample.dna" @file_data = get_file_data("sample.dna"); # Extract the sequence data from the contents of the file "sample.dna" $dna = extract_sequence_from_fasta_data(@file_data); # Translate the DNA to protein in six reading frames # and print the protein in lines 70 characters long print "\n -------Reading Frame 1--------\n\n"; $protein = translate_frame($dna, 1); print_sequence($protein, 70); print "\n -------Reading Frame 2--------\n\n"; $protein = translate_frame($dna, 2); print_sequence($protein, 70); print "\n -------Reading Frame 3--------\n\n"; $protein = translate_frame($dna, 3); print_sequence($protein, 70);

# Calculate reverse complement $revcom = revcom($dna); print "\n -------Reading Frame 4--------\n\n"; $protein = translate_frame($revcom, 1); print_sequence($protein, 70); print "\n -------Reading Frame 5--------\n\n"; $protein = translate_frame($revcom, 2); print_sequence($protein, 70); print "\n -------Reading Frame 6--------\n\n"; $protein = translate_frame($revcom, 3); print_sequence($protein, 70); exit; Note: BeginPerlBioinfo.pm is a library of subroutines. Its available in the online. Its downloaded from the website http://www.math.lsa.umich.edu/~dburns/548/BeginPerlBioinfo.pm The modules are placed in the same directory of the perl. Then this module can be used in this script.

Output: [sushmita@ltspbinfo1 ~]$ perl 6ORF.pl -------Reading Frame 1-------QRSTISRSSSPA -------Reading Frame 2-------KGRLLAAVVLQL -------Reading Frame 3-------KUDY_PQ_FSSW

-------Reading Frame 4-------PAGELLRLIVDL -------Reading Frame 5-------QLENYCG_STF -------Reading Frame 6-------SWRTTAANSRPL sample.dna files : >gi|416090402|dbj|HV931356.1| JP 2011509269-A/1: Novel insulin derivatives having an extremely delayed time-action profile CAAAGGTCGACTATTAGCCGCAGTAGTTCTCCAGCTGG

(14) Write a subroutine to determine the nucleotide / residue count in a sequence passed on to an argument and use it in a script the number of residue in a sequence. (15) Read in a sequence from the user to determine if the given sequence is palindrome or not.
#!/usr/bin/perl -w #checking the sequence palindrome or not print "Enter your sequence"; chomp($seq=<STDIN>); $rev=reverse($seq); if($seq eq $rev) { print "$seq is a palindromic sequence"; } else{ print "$seq is not a palindromic sequence"; } exit;

ASSIGNMENT ON PERL PROGRAMING (REGULAR EXPRESSIONS)

(1) #!/usr/bin/perl w #replace all T with U print "Enter your genbank filename:\n"; chomp($in=<STDIN>); open(RR,"$in")||die("error"); open(WW,"+>$in")||die("error"); while($line=<RR>) { $line =~ s/Tt/Uu/g; print WW $line; } close(RR); close(WW); exit;

(2_a) #!/usr/bin/perl w #retrieve three lines that contain a three letter string consisting of t then any character then e

print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/t.e/i) { print "$line\n"; } } close(FH);

(2_b) #!/usr/bin/perl w #retrieve three lines that contain a three letter word that starts with t and end with e print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/t.?e\b/i) {

print "$line\n"; } } close(FH); (2_C) #!/usr/bin/perl w #retrieve the lines that contain a word of any length that starts with t and end with e so that word has at least three characters. print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/^t\w{3,}.*e\b/i) { print "$line\n"; } } close(FH);

(2_d) #!/usr/bin/perl w #retrieve the lines that start with a and ends with n print "Enter the filename:";

$filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/^a.*n\b/i) { print "$line\n"; } } close(FH); (2_e) #!/usr/bin/perl w #retrieve the blank lines print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/^\s$/i) { print "$line\n"; } }

close(FH);

(2_f) #!/usr/bin/perl w #retrieve lines that have two o print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/oo/gi) { print "$line\n"; } } close(FH); (2_g) #!/usr/bin/perl w #retrieve lines that donot contain the blank space characters print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) {

chomp($line); if($line!~m/\s/i) { print "$line\n"; } } close(FH); (2_h) #!/usr/bin/perl w #retrieve lines that contain more than one blank space character print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/\s{2,}/i) { print "$line\n"; } } close(FH); (2_i) #!/usr/bin/perl w #retrieve lines that contain a letter followed by a non-letter

print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/\w\D\d/i) { print "$line\n"; } } close(FH); (2_j) #!/usr/bin/perl w #retrieve lines that containing a word that starts with upper case print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/^[A-Z]/) { print "$line\n";

} } close(FH); (2_k) #!/usr/bin/perl w #retrieve lines that containing a date. print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~m/\d.\d.\d/) { print "$line\n"; } } close(FH); (3) #!/usr/bin/perl w #retrieve lines with set of binary string print "Enter the filename:"; chomp($file=<STDIN>); print"\n 1. for printing lines of all binary strings\n 2. for printing

lines of all binary string except empty string\n 3. for printing lines begins with 1 and ends with 1\n 4. for printing lines of binary strings ends with 00\n 5. for printing lines contains atles three 1s\n"; print "Enter the option:"; chomp($op=<STDIN>) open(RH, "$file")||die"could not open file"; @ar=<RH>; chomp(@ar); if ($op==1) { foreach $e(@ar) { if ($e=~m/[0-1]/) { print "$e\n"; } } } elsif ($op==2) { foreach $e(@ar) { if ($e=~m/!\s[0-1]/) { print "$e\n"; }

} } elsif ($op==3) foreach $e(@ar) { if ($e=~m/^1.*1$/) { print "$e\n"; } } } elsif ($op==4) { foreach $e(@ar) { if ($e=~m/00$/) { print "$e\n"; } } } elsif ($op==5) { foreach $e(@ar)

{ if ($e=~m/(111)/) { print "$e\n"; } } } else { print "Invalid option"; } (4) #!/usr/bin/perl w #retrieve lines that containing all five vowels print "Enter the filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if(($line=~/a/) && ($line=~/e/) && ($line=~/i/) && ($line=~/o/)&& ($line=~/u/)) { print "$line\n"; }

} close(FH);

(5) #!/usr/bin/perl w #Read a .pdb file and extract the name of the atoms & their coordinates print "Enter the pdb filename:"; $filename=<STDIN>; open(FH,$filename); while($line=<FH>) { chomp($line); if($line=~/^ATOM/) { print "$line\n"; } } close(FH);

Vous aimerez peut-être aussi