Home Perl
Search and replace within multiple files

find . -iname "<filePatern>" |xargs perl -pi -w -e 's/<find>/<replace>/g';


Date 

(my $sec, my $min, my $hour, my $mday, my $mon, my $year,my $wday, my $yday, my $isdst)=localtime(time);
my $FullDate = sprintf("%4d-%02d-%02d %02d:%02d" ,$year+1900, $mon+1, $mday, $hour, $min);


Unix timestamp to human readable

perl -e “print scalar(localtime($UNIX_TIMESTAMP))”


Loading the output of a system command into an array

my @array;

open MYCOMMAND, "ps auxw"; 
  @array = <MYCOMMAND>; 
close MYCOMMAND;


Loading a file into an array

my @array;

open (myFile, "/path/file") || die ("Could not open file");
  @array =  <myFile>;
close (myFile);
chomp(@array);


write into a system command that waits for input and exits with enter

open MYCOMMAND, "myexec";
  print MYCOMMAND "this is some content";
close MYCOMMAND;


Write into file

my $myFile = "/tmp/myFile";

// > new and overrite, >> append to file
open(OUT, ">> $myFile") || die "Could not open $myFile";
for my $oneline (@myarray) {
   print OUT $oneline . "\n";
}
close(OUT);


Delete files

for my $file (glob("$path/*.old"))
{
  unlink($file);
}