Info

I often use the following arguments to perl:

Perl One-Liners

One-Liner: Count the number of times a specific character appears in each line

One-Liner: Add string to beginning of each line

One-Liner: Add newline to end of each line

One-Liner: Print only some columns of a file

One-Liner: Print all columns except the first

One-Liner: Replace a pattern with another one inside the file with backup

One-Liner: Print only non-uppercase letters

One-Liner: Print one word per line

One-Liner: Kill all screen sessions (no remorse)

One-Liner: Return all unique words in a text document (divided by spaces), sorted by their counts (how often they appear)

One-Liner: Delete all special characters

One-Liner: Lower case everything

One-Liner Combination: Combine lower-casing with word counting and sorting

One-Liner: Print only one column

One-Liner: Print only text between tags

One-Liner: Sort lines by their length

One-Liner: Print second column, unless it contains a number

One-Liner: Trim/ Collapse white spaces and replace new lines by something else

One-Liner: Get the average of one column from certain lines

One-Liner: How to sort a file by a column

One-Liner: Replace specific space but also copy a group of matches

More info

Sending multiple commands to screen session on different machines

  • #!/usr/bin/perl -w

    # This script creates screen sessions, ssh's to machines and executes code on these machines.
    # parameters: -s (start) -r (run) -q (quit)
    # HowTo:
    #  1) change the executed code, property folder and prefix to your values
    #  2) select your machines
    #  3) on the machine where you want your screen sessions run to start your sessions: ./clusterSubmitJobs.pl -s
    #  4) once you're done and want to quit all your sessions: ./clusterSubmitJobs.pl -q
    # author: richard socher.org

    use strict;
    use Getopt::Std;
    use List::Util qw[min max];
    my %options=();
    getopts("srq",\%options);

    #------------------
    # files to be considered
    my $folder = '/folderWithInputFiles';
    my $prefix = 'tests_';
    my $ext = '.config';
    # code to run with files
    my $code = './runMyScript.sh -configFile ';

    # deprecated by mstat
    my @freemachines = ('machine1.yourPlace.edu', 'machine2.yourPlace.edu');
    #-------------------


    my $full = $folder . $prefix . '*' . $ext;
    print "Using files: $full \n";

    my @files = <$full*>;
    my $numMachines = @freemachines;
    my $numFiles = @files;
    my $minNum = min($numMachines,$numFiles);

    for (my $i = 0; $i < $minNum; $i++) {
      if ($options{s}){
        print "Creating screen session: freemachines[$i] for \t $files[$i] \n";
        system("screen -d -m -S $freemachines[$i]");
        system("screen -S $freemachines[$i] -p 0 -X stuff \"ssh $freemachines[$i]\015\"");
      }

      if ($options{r}){
        print "run: screen -S $freemachines[$i] -p 0 -X stuff \"$code $files[$i]\"\n";
        system("screen -S $freemachines[$i] -p 0 -X stuff \"$code $files[$i]\015\"");
      }

      if ($options{q}){
        print "screen -S $freemachines[$i] -p 0 -X stuff \"exit\n";
        system("screen -S $freemachines[$i] -p 0 -X stuff \"exit\015\"");
        system("screen -S $freemachines[$i] -p 0 -X stuff \"exit\015\"");
      }
    }

How to install new CPAN modules?

  • perl -MCPAN -e shell # go to CPAN install mode
    install Bundle::CPAN # update CPAN
    reload cpan
    install Set::Scalar
Add Comment 
Sign as Author 
Enter code: