Linux Multiple File Search And Replace

This is something I’ve needed to do occasionally and always have to look up. The one I like best is located at this post but I’m going to repeat it here for posterity and so I can find it again easier.

perl -pi -w -e 's/search/replace/g;' *.php

So easy, thats why Perl rocks. Thank you Bob Fulkerson for introducing us.

Other options include the frickin sweet looking ruby app SearchAndReplaceGlobal by the same guy who wrote the editor I first learned to do HTML with, Arachnophilia. I say sweet looking because I never actually got it running. I didn’t try very hard though.

Posted January 21st, 2007 - Permalink
Categories: Linux - Perl - Programming - Snippets
No Comments »
 
Cleaning Up E-Books

I have a large number of ebooks in Microsoft’s .lit format. My Nokia 770 doesn’t have any software to read a .lit format book. In fact, I can’t say I’ve ever seen a .lit reader other than Microsoft’s own.

What I have seen is the nifty and very usefull ConvertLIT which I use to down convert the files into plain HTML. I don’t even bother with the images. The problem is, they tend to come out formatted in a hideous fashion. I came up with a nice combo of HTML tidy and a perl script.

Here’s my command line for tidy, beware, this will modify your original copy!

tidy --bare yes --clean yes --drop-font-tags yes --drop-proprietary-attributes yes --enclose-text yes --output-xhtml yes --word-2000 yes --tidy-mark no --write-back yes TARGETFILENAME.htm

Here is my perl script, it just runs the file through some regex’s and writes to the same filename with “NEW” appended. I also made a nice little progress bar because I was bored.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl
 
$file = $ARGV[0];   # Name the file
open(INFO, "< ".$file);   # Open the file
@lines = <INFO>;    # Read it into an array
close(INFO);      # Close the file
 
$size = @lines;
$counter = 0;
$size = $size / 50;
 
open(FILEWRITE, "> NEW".$file);
foreach(@lines) {
  $counter++;
  if(0 == ($counter % 50) || $counter == @lines) {
  print "\rProcessing: [";
  for($i = 0; $i < ($counter / $size); $i++) {
    print "+";
  }
  for($i = 0; $i < (49 - ($counter / $size)); $i++) {
    print "-";
  }
  print "]";
  }
 
  # Empty paragraph removal
  $_ =~ s/<p>\s*<\/p>//mi;
  if($_ =~ m/^\s*\n$/) {
    # If the line is just a newline or newline and spaces, scrap it.
    $_ = '';
  }
  else {
    # Remove excess spaces
    $_ =~ s/  //mi;
    # I get these alot...
    $_ =~ s/&shy;//mi;
  }
  print FILEWRITE $_;
}
close FILEWRITE;
print "\n";

You can download it here, but be careful with it.
cleaner.pl.txt

Update (01/21/07)
That perl script has a line $_ =~ s/ //mi; which doesn’t really make that much sense looking at it now. I’m thinking $_ s/\s\s+/ /mi; for a replacement. Also, for some reason the server throws up a 500 error on trying to get that file, I’m working on it.

Posted January 19th, 2007 - Permalink
Categories: Nokia 770 - Perl
No Comments »
 
sed, perl, rename

This is a post from a previous system. Information, links and images may not be vaild.

Yesterday I spent 10, maybe 20 minutes poking around the internet looking for some sort, any sort, of batch renaming utility for linux. I knew there had to be one somewhere, I mean, come on. I eventually found a batch file that ran into some sed that wasn’t really what I wanted, but I figured I could pick apart the sed and figure it out.

Bad idea, I ran it without really testing it, and it ate all of my files, every single one. Luckily I had backups, and I started pulling them off the server. Today I wrote a Perl script to do the job, and finished off with one more Google search to see if I couldn’t find something simpler.

I guess my problem was searching for “batch rename” because there were no good results, but I found the nix command rename this time and check out the man page. Wow, stupid me, I re-invented the wheel today,and didn’t do it nearly as well.

Whatever, I can live with that, and it got me writing some Perl, which I haven’t done in a long time. So here for your consumption is my Perl script that will let you blow your foot off, but can get the job done for substitution.

#!/usr/bin/perl  if($#ARGV < 2) {   print "Directory: ";   $directory = <>;   chomp($directory);   print "Replace: ";   $find = <>;   chomp($find);   print "With: ";   $with = <>;   chomp($with); } else {   $directory = $ARGV[0];   $find = $ARGV[1];   $with = $ARGV[2]; }  opendir(DIR, $directory) || die "can't opendir $directory: $!"; @dots = readdir(DIR); closedir DIR;  foreach(@dots) {   $copy = $_;   if( $_ =~ s/$find/$with/ )   {     system("mv $copy $_");   }   }
Posted August 17th, 2006 - Permalink
Categories: Linux - Perl
No Comments »