# This is more of a code snippet to include in programs. # It provides for insert, delete, and change functionality to lines of text # within given files. Each of the subroutines takes a set of paramaters to invoke it. # Text below documents the usage for each of the subroutines # # insert_line uses three paramaters, the first of which is the filename to insert a line into, # the second which is the line after which you want the input inserted into, and the third which # is the text you want inserted into the file. # # search_replace uses three parameters, the first being the filename to be worked on, the second # being the text to search for, and the third being the text to replace the searched text with. # # delete_line uses two paramaters, the name of the file to delete the text from, and the name of # the text to strike from the file. Below is invocation of delete_lines function example # # my $filename="file"; # the filename # my $delete="Hello"; #Things to search for to insert after # my $replace="Goodbye!"; #Thing to insert. # delete_line($filename,$delete); # # to enable searching for perl regular expressions, here is an invocation example: # my $search=qr{perl regular expressions}; e.g. \d+ #Uncomment the code below to call the function # # #This subroutine takes 3 paramaters, 1. the file to edit, 2. the thing to search for, 3. text # to be inserted after the found item. It backs up the old file, creates a new file, and then #the new file moviing the old file to .bak. ## sub insert_line { my $file=$_[0]; # Name of file to edit my $old = $file; # Old file my $new = "$file.tmp.$$"; #New File to create my $bak = "$file.bak"; #backup of old file my $after = $_[1]; #the thing to insert the line after (text) my $insertline = $_[2]; #the thing to insert itself $old = $file; $new = "$file.tmp.$$"; $bak = "$file.bak"; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while () { if (/$after/) { $_ .= $insertline; } (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old); } # #this subroutine changes one thing to another in a file. #It takes 3 paramaters, The filename, the thing to find, and the thing to replace. sub search_replace { my $file= $_[0]; my $old = $file; my $new = "$file.tmp.$$"; my $bak = "$file.bak"; my $find = $_[1]; my $replace = $_[2]; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while () { s/$find/$replace/g; (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old); } # #This subroutine deletes a line in a file. It takes two arguments, the filename to operate on, #and line containing the text to be deleted. # sub delete_line { my $file= $_[0]; my $old = $file; my $new = "$file.tmp.$$"; my $bak = "$file.bak"; my $delete = $_[1]; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while () { if (/$delete/) { $_= ""; } (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old);