in reply to Re: Remove lines from a list of files based on those in a given file
in thread Remove lines from a list of files based on those in a given file

SHORT STORY ALL FIXED THANKS Sorry for horrible formatting but Perl Monks makes posting a pain (all + those tages I cant remember hidden in the site so finding how to pos +t takes 10 minutes I'll never get back each time). Thanks for taking a look. Here is what I've found. The file that con +tains the matches has loads of drives like this \\HOFIL01\CALDWELLS$ \\GEMINI1\CAMBRIDGE$ \\GEMINI1\ADMIN \\GEMINI2\CONFERENCE \\HOFIL02\PROJECT \\HOIFL02\CURRFUNC \\HOFIL01\CAPELLAC$ \\HOFIL01\CARKEEKF$ When the script hits a line like \\HOFIL02\PROJECT it breaks. If I ch +ange it to \\HOFAL02\PROJECT or \\HOFIL02\PROJECT THIS or \\HOFIL02\P +Z (but no lower letter than Z) it will work (or stop on the next \\ho +fil02\p(a-y) (but if it ended in $ it was ok?! in horror at the oddness of this I simply removed all \,$ and spaces f +rom the variables before comparing them which worked fine. How bizza +r? What would cause only lines starting in \\hofil02\p(a-z)$ to caus +e an issue? Also for a start I just removed any space from the varia +bles. Then it would stop on half the lines starting in \\hofil02\p(a +-z)???$ (eg \\hofil01\pathis$). The Dat file that these patterns where being matched against looked li +ke this. [NATIONAL OFFICE] floor=2 h:=\\moent14\username$ i:=\\aknt1\operations The dats had no space on the end of the drive entries but the matching + file did. All very odd. My Perl was always bad and now it is rust +so here is my current full script (no laughing please it works nicely +). I'll mod it up to output the changed files (and eventually to rep +lace the existing ones after some more testing). # Once more into the Perl Dear Friends Bruce Taylor :: Datacom Co +nsulting # # Search all MOE DAT files in a given directory, remove any lines fou +nd in another given file use File::Find; # Built in perl function that finds files i +n directories (find - traverse a file tree) # http://www.perldoc.com/perl5.6.1/lib/File/Find.html $feeddir = @ARGV[0]; # @ARGV is the built in perl array that is + feed to the script from the command line $feeddir =~ s/\\/\//; # If the dir is given using \ (like c:\tem +p) then subsitute all \ for /'s if ($feeddir =~ "") { system("cls"); print "\n\n\n\nNo directory specified\n\n"; print "You need to specify a driectory to search down\n"; print "EG... SearchReplace.pl c:\\TEMP\DATS\n\n\n\n\n"; die; } chdir("$feeddir"); # Change working directory to the now co +rrected directory given on the command line find(\&wanted, "$feeddir"); # use the built in find command wit +h the wanted subroutine and send it the $feeddir variable sub wanted # the wanted subrouting is used to select o +nly .dat files(further) down and set the filename { # the { is used to start a code block /\.DAT$/i or return; # Nab only DATS or go to the n +ext file $filename = $File::Find::name; # set $filename variable to +full path\name ie c:\temp\dats\datfile.dat &Details($filename); # so we have the file and then fee +d that into the Details sub/sub routine } sub Details { # the Details sub $file = shift; # Open the file containing the matches to remove open(BADSHARES, "c:/testy/Bad_Shares.txt"); @BadShares = <BADSHARES>; close BADSHARES; # Open the file/s to remove these matches from open(CURRENTDAT, "$file"); @DatLines = <CURRENTDAT>; close CURRENTDAT; # For each line of the file containing the matches to remove for ($a = 0; $a < scalar(@DatLines); $a++) { # Go through each line of the file you want to remove the matches +from for ($i = 0; $i < scalar(@BadShares); $i++) { # Remove any \, $ or space from the line being worked on from each + file (seems to upset perl in unnatural ways) $BadShares[$i] =~ s/ //g; $BadShares[$i] =~ s/\\//g; $BadShares[$i] =~ s/\$//g; $DatLines[$a] =~ s/ //g; $DatLines[$a] =~ s/\\//g; $DatLines[$a] =~ s/\$//g; next if (!($DatLines[$a] =~ m/($BadShares[$i])/i)); chomp $DatLines[$a]; chomp $BadShares[$i]; print "$file $DatLines[$a] $a $BadShares[$i] + $i\n"; } } }
  • Comment on Re^2: Remove lines from a list of files based on those in a given file
  • Download Code

Replies are listed 'Best First'.
Re^3: Remove lines from a list of files based on those in a given file
by GrandFather (Saint) on Jan 31, 2006 at 02:36 UTC

    The tags you complain of are HTML or PerlMonks special tags. Actually, there are only three tags that you really need to know - <p>, <code> and <readmore>. You can get away without closing paragraph tags (<p>), but you should really close code and readmore tags: </code> and </readmore>.

    When you preview a node you are posting, there is a link in the fine print to Writeup Formatting Tips. As the advise says: " If something looked unlike you expected it to you might need to check out Writeup Formatting Tips"

    Now you can go back and put those paragraph tags in that you missed :)


    DWIM is Perl's answer to Gödel
      This formatting you speak of blows us from common sense, annoyance shall come and people shall arrive not at all.

      The Code tags make code hard to read. The paragraph tags seem quite redundant given the enter key (you know the one you hit it after typing in the paragraph tag). That readmore tage does look interesting.

      Still you are correct and I'll tidy it as the community is so excellent as to make up for this "crazyness." The small print yes I agree every few months when I pop in (having forgotten my account details) I find it eventually (untill the next time).

      Sigh an argument I can never win and should never have started ;).

      /cap in hand/ I'll go back and clean my mess, apparently I sinned with a perl earlier when it should naturally be Perl.

      I bet you call have Mac's running Linux <BG>