|
|
Subject:
Recursive string matching utility
Category: Computers > Programming Asked by: cereb-ga List Price: $30.00 |
Posted:
29 Sep 2004 01:38 PDT
Expires: 29 Oct 2004 01:38 PDT Question ID: 407818 |
|
Subject:
Re: Recursive string matching utility
Answered By: palitoy-ga on 29 Sep 2004 03:21 PDT Rated: |
Hello cereb I have written a small perl script for you to perform this task. It checks file1 for sentences and then removes them from file2, this then leaves file2 with only the different lines (and this is output as file3). I always try to make my script as readable as possible for others (I could make it a lot more unreadable if you wish as Perl is good at that!) and within the script you need to alter the three filenames to the required values (this is near the top of the script). I have made the assumption that differences in whitespace (such as multiple spaces) are not important but differences in CasiNg is. If you require any further help or need any modifications please ask for clarification and I will try to respond as swiftly as possible. ### #!/usr/bin/perl # comment the next line out if run from a command line print "Content-type: text/html\n\n"; # some declarations use strict; # these are the filenames of the files and should be editted. # $file3 is the file that is output after editting. my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $file3 = "output.txt"; # read in the first file into a variable called $string open FILE, $file1 or die "Couldn't open file: $!"; my $string = join("", <FILE>); close FILE; # remove multiple spaces from $string $string =~ s/\s{1,}/ /gi; # split the $string into sentences separated by . into an array my @lines = split /\./, $string; # read in the second file into a string open FILE, $file2 or die "Couldn't open file: $!"; $string = join("", <FILE>); close FILE; # remove multiple spaces from $string $string =~ s/\s{1,}/ /gi; # loop through the sentences in file1 (now stored in the array @lines) foreach ( @lines ) { # delete the line from file2 using a regular expression if it exists # this is *not* case sensitive but can be made so if necessary $string =~ s/$_\.//g; }; # remove multiple spaces from $string which now holds the different lines $string =~ s/\s{1,}/ /gi; # split the string into sentences so that when it is # printed it has one sentence per line my @output = split /\./, $string; # output the differences open (OUT, ">$file3"); foreach my $line ( @output ) { # remove leading whitespace $line =~ s/^\s+//; # print to file print OUT "$line.\n"; # print to screen print "$line.\n"; }; close(OUT); # exit the program exit(0); ### | |
| |
|
cereb-ga
rated this answer:
Thank you, that was quick. A desktop solution would have been best, but you were responsive and I trust it will work. |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |