Tuesday, July 3, 2018

perl scripts3

1. addstring.pl

#!/cadappl/bin/perl -w

use strict;

if (($ARGV[0] eq "-h")  or ($ARGV[0] eq "-help"))
{
  print "USAGE      :: /psc/proj/ltg/PCS/users/aurnobc/scripts/perl/addString.pl \n";
  print "DESCRIPTION:: New line containing will be added after line that matches in \n";
  exit;
}

my $file    = $ARGV[0];
my $string  = $ARGV[1];
my $addLine = $ARGV[2];

my $tempfile = "temp.file";

open INFILE, "<$file" or die "Cannot open $file: $!\n";

open OUTFILE,">$tempfile" or die "Cannot open $tempfile: $!\n";
my $line;
my $instance = 0;
my $replaced = 0;

foreach $line ()
{
  if ($line !~ m/$string/)
  {
    print OUTFILE $line;   
  }
  elsif ($line =~ m/$string/)
  {
    $line = $line."$addLine\n";
    print OUTFILE $line;
    $replaced = 1;
    $instance++;   
  }
}
close INFILE;
close OUTFILE;

print "$instance line(s) containing $addLine added\n";

die ("Could not move $tempfile to $file: $!") if (system ("mv $tempfile $file"));

2. removestring.pl

#!/cadappl/bin/perl -w

use strict;

if (($ARGV[0] eq "-h")  or ($ARGV[0] eq "-help"))
{
  print "USAGE      :: /psc/proj/ltg/PCS/users/aurnobc/scripts/perl/removeString.pl \n";
  print "DESCRIPTION:: Line containing in will be removed\n";
  exit;
}

my $file   = $ARGV[0];
my $string = $ARGV[1];

my $tempfile = "temp.file";

open INFILE, "<$file" or die "Cannot open $file: $!\n";

open OUTFILE,">$tempfile" or die "Cannot open $tempfile: $!\n";
my $line;
my $instance = 0;

foreach $line ()
{
  if ($line !~ m/$string/)
  {
    print OUTFILE $line;   
  }
  else
  {
    $instance++;
  }
}
close INFILE;
close OUTFILE;

print "$instance line(s) containing $string removed\n";

die ("Could not move $tempfile to $file: $!") if (system ("mv $tempfile $file"));

3. ReplaceString.pl

#!/cadappl/bin/perl -w

use strict;
use File::Basename;

if (($ARGV[0] eq "-h")  or ($ARGV[0] eq "-help"))
{
  print "USAGE      :: replaceString.pl \n";
  print "DESCRIPTION:: All instances of will be replaced by in (including file name)\n";
  exit 0;
}

my $file    = $ARGV[0];
my $string1 = $ARGV[1];
my $string2 = $ARGV[2];

my $tempfile = basename($file);
my $srcfile  = $file;
#my $srcdest = 0;
my $srcdest = (defined $ARGV[3] && $ARGV[3] eq "update") ? 1 : 0;

$tempfile =~ s/$string1/$string2/g;
$srcfile  =~ s/$string1/$string2/g;

if ($tempfile eq $file)
{
  $tempfile = "temp.file";
  $srcdest = 1;
}

open INFILE, "<$file" or die "Cannot open $file: $!\n";

open OUTFILE,">$tempfile" or die "Cannot open $tempfile: $!\n";
my $line;
my $instance = 0;

foreach $line ()
{
  if ($line =~ m/$string1/)
  {
    $line =~ s/$string1/$string2/g;
    $instance++;   
  }
  print OUTFILE $line;
}
close INFILE;
close OUTFILE;

print "$instance line(s) containing $string1 replaced by $string2\n";

if ($srcfile eq $tempfile)
{
  die ("Could not delete $file: $!") if (not (unlink $file));
}
elsif ($srcdest)
{
  die ("Could not move $tempfile to $file: $!") if (system ("mv $tempfile $file"));
}


4. removeBlock.pl

#!/cadappl/bin/perl -w

use strict;

if (($ARGV[0] eq "-h")  or ($ARGV[0] eq "-help"))
{
  print "USAGE      :: /home/plt/stdcelleda/workspaces/aurnob/CMOS14/removeBlock.pl \n";
  print "DESCRIPTION:: and lines respectively above and below line containing in will be removed (excluding the matched line)\n";
  print "DESCRIPTION:: Supported values for and are whole numbers, \"all\"\n";
  exit;
}

if ($#ARGV != 3)
{
  system("/home/plt/stdcelleda/workspaces/aurnob/CMOS14/removeBlock.pl -h");
  exit;
}

my $file   = $ARGV[0];
my $string = $ARGV[1];
my $aLines = $ARGV[2];
my $bLines = $ARGV[3];

if (($aLines =~ /\D+/ && $aLines ne "all") or ($aLines =~ /\d+/ && $aLines < 0))
{
  print "ERROR      :: LINES_ABOVE cannot contain $ARGV[2]\n";
  system("/psc/proj/ltg/PCS/users/aurnobc/scripts/perl/removeBlock.pl -h");
  exit;
}

if (($bLines =~ /\D+/ && $bLines ne "all") or ($bLines =~ /\d+/ && $bLines < 0))
{
  print "ERROR      :: LINES_BELOW cannot contain $ARGV[3]\n";
  system("/psc/proj/ltg/PCS/users/aurnobc/scripts/perl/removeBlock.pl -h");
  exit;
}

my $tempfile = "temp.file";

open INFILE, "<$file" or die "Cannot open $file: $!\n";
open OUTFILE,">$tempfile" or die "Cannot open $tempfile: $!\n";
my ($line, @array);
my $i = 0;

my @lines = ;
my $orgLines = scalar(@lines);
close INFILE;;

foreach $line (@lines)
{
  if ($line !~ m/$string/)
  {
    if ((defined $array[$i]) and ($array[$i] ne "not defineD!") or (not defined $array[$i]))
    {
      $array[$i] = $line;       
    }
    else
    {
      undef $array[$i];
    }    
  }
  else
  {    
    $array[$i] = $line;       

    if ($aLines eq "all")
    {
      for (my $a = 0; $a <= $i; $a++)
      {
        $array[$a]=undef;
      }
    }
    else 
    {
#changed init: $a = 0 to $a =1
      for (my $a = 1; $a <= $aLines; $a++)
      {
        last if ($a > $i);
$array[$i-$a]=undef;
      }    
    }
    last if ($bLines eq "all");
    for (my $a = 1; $a <= $bLines; $a++)
    {
      $array[$i+$a]="not defineD!";
    }
  }
$i++;
}

my $newLines = 0;
foreach(@array)
{
 if(defined($_) and ($_ ne "not defineD!"))
 {
   $newLines++;
   print OUTFILE "$_";   
 }
}

my $linesRem = $orgLines - $newLines;

close OUTFILE;
print "Block of $linesRem line(s) removed\n";
die ("Could not move $tempfile to $file: $!") if (system ("mv $tempfile $file"));

5.  fname.pl (file realpath)

##############################################
# Usage: perl