Collection of mostly command line tools / PHP scripts. Somewhat out of date.

⌈⌋ ⎇ branch:  scripts + snippets


Artifact [ab56e72cda]

Artifact ab56e72cda20521d7fe06742c10eab9ab165292e:

  • Executable file fr — part of check-in [c66385ed29] at 2012-01-09 04:14:29 on branch trunk — file replace cmdline tool (user: mario size: 1008)

     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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
#!/usr/bin/perl -w

## require("/home/mario/public_html/x/sh-lib.pl");

($PATTERN, $REPLACEMENT, @files) = @ARGV;

if ($PATTERN && $REPLACEMENT && @files) {


if ($#files < 0) {
   @files = ("-");
}
elsif (!$files[0] eq "-") {
   print "fr: replacing `$PATTERN' with `$REPLACEMENT'\n";
}


foreach $file (@files) {

  if ( (! ($file eq "-")) && (! -f $file || ! -r $file) ) {
     next;
  }

  open READ, "$file";
  @text = (<READ>);
  close READ;

  $text = '';
  foreach $line (@text) {
    $text .= $line;
  }

  @numfound = ($text =~ /($PATTERN)/igs);
  $numfound = $#numfound + 1;
  if ($numfound >= 1) {

    $text =~ s/$PATTERN/$REPLACEMENT/igs;

    if (! ($file eq "-")) {
       print "$numfound replacing in $file\n";
    }

    open WRITE, ">$file";
    print WRITE $text;
    close WRITE;

  }
}

}
else {
  print "\nfr PATTERN REPLACEMENT files...\n\nwill replace any occourences of ".
        "PATTERN with REPLACEMENT in given files\n(use `find` in place of ".
        "\"files...\")\n\n";
}