#!/usr/bin/perl ############################################################################### # # name: strSub.pl # # usage: strSub.pl -fht # [ ... ] # # decription: substitute with in all files. # with option -f the script will work like a generic multilines # text substitution, and will be loaded from the # files and . # # author: paolo guiducci # # Disclaimer: You are free to use this code, being understood that I decline # any responsibility, direct or implied, for damages or # inconveniences that its use or impossibility of use may cause # (directly or indirectly). If your local law does not allow a full # decline of responsibility by the author of a software that you # use, then you are not allowed to use this code or any part of it. # # web: http://pguiducci.com ############################################################################### use Getopt::Std; getopts("fht") or usage(); if ($opt_h) {usage()}; my $par1 = shift(@ARGV); my $par2 = shift(@ARGV); my $str1; my $str2; my @par; my $fl=0; my $ind=0; if ($opt_f) { open (PAR, "<$par1") or die "unable to open $par1"; @par = ; $str1 = join("",@par); chomp($str1); close PAR; open (PAR, "<$par2") or die "unable to open $par2"; @par = ; $str2 = join("",@par); chomp($str2); close PAR; } else { $str1=$par1; $str2=$par2; } foreach $file (@ARGV) { if (!-w $file) { print "warning: $file is not writable"; next; } open (IN, "< $file") or die "unable to open $file"; @fileIn = ; close IN; $fileSca = join("",@fileIn); $fileOut = $fileSca; $fileOut =~ s/$str1/$str2/g; if ($fileOut ne $fileSca) { if ($opt_t) { print "test update file: $file\n"; } else { open (OUT, "> $file") or die "unable to open $file in output mode"; print OUT $fileOut; close OUT; print "updated file: $file\n"; } } else { print "file not updated: $file\n"; } } sub usage { print "usage: strSub.pl -fht [ ... ]\n"; exit; }