Using substitutions to clean up input data. : substitution « Regular Expression « Perl






Using substitutions to clean up input data.

    

#!/usr/bin/perl -w 
foreach $i (0 .. $#ARGV) {
     open(INPUT, $ARGV[$i]) or die "Can't open $ARGV[$i].";
     while (<INPUT>) {
          $string = $_;
          remove_trailing($string);
          remove_leading($string);
          print "$string\n";
     }
     
     close(INPUT);
}

sub remove_leading {
     my($string) = $_[0];
     
     $string =~ s/^\s+//;
     $_[0] = $string;
}
sub remove_trailing {
     my($string) = $_[0];
     
     $string =~ s/\s+$//;
     $_[0] = $string;
}

   
    
    
    
  








Related examples in the same category

1.Options for the substitution operator.
2.Replace pattern
3.Replace pattern with ()
4.Substitute I with N and print
5.Substitute N with J
6.Substitute every T with M
7.Substitute first T with an M
8.Substitute first occurrence of "blue" with "red"
9.Substitute tom with Mary
10.Substitution Example
11.Substitution Modifiers
12.Capitalize all sentences
13.Using Patterns with Substitutions
14.Changing substitution delimiters
15.Increase salary with pattern match
16.Evaluate replacement
17.Alternation: /John|Karen|Steve/ will match a line containing John or Karen or Steve.
18.Options for the S Operator
19.Ignore case, global substitution