eTechtips

Tech Tips and Insights

Category: Software

  • Perl: Open and read file line by line

    I regularly have to process Comma Seperated Value or CSV files, usually for user lists. So the following perl code is a framework for processing a file line by line.

    #!/usr/bin/perl
    use strict;
    my $line;
    
    open(FILEIN, "filename.txt");
    
    while(<FILEIN>)
    {
      chomp();
      $line = $_;
      # Process $line for data
      # Example seperating line by commas: 
      my ($username,$firstname,$lastname,$email) = split(/,/,$line);
    }
    
    close(FILEIN);
    
    0 Comments
  • Perl: Hashes

    Here are some basics about Perl hashes that can be helpful to be used when an you need to associate a label with some value such as for names and telephone numbers or account numbers with amounts owed.

    Create a hash variable.

    my %hashvar;
    

    Create a hash reference

    my $refhash = {};
    

    Add a value to a hash variable.

    $hashvar{'somekey'} = 'someval';
    

    Add a value to a hash reference.

    $hashref->{'somekey'} = 'someotherval';
    

    Access a value in a hash.

    print $hashvar{'somekey'};
    

    Access a value in a hash reference.

    print $hashref->{'somekey}';
    

    Access all Keys in a hash variable.
    foreach my $k (keys(%hashvar))
    {
    print $hashvar{$k};
    }

    Access all Keys in a hash reference.

    foreach my $k (keys(%$hashref))
    {
      print $k . "n";
    }
    

    Access all Keys and values in a hash.

    while (my ($key,$value) = each %hashvar)
    {
      print "Key: " . $key . " has value:" . $value . "n";
    }
    

    Access all Keys and values in a hash.

    while (my ($key,$value) = each %$refhash)
    {
      print "Key: " . $key . " has value:" . $value . "n";
    }
    

    Assigning multiple values to a hash

    %hasvar = (
      'test2' => 'myval',
      'test3' => 'myval3'
    );
    

    Note: This will clear the existing hash.

    Delete a value from a hash

    delete $hashvar{'key1'};
    

    Delete a value from a hash reference.

    delete $hashref->{'key1'};
    
    0 Comments
  • Linux System utilization tools

    I was asked about system utilization on Linux systems. I found thesystatset of tools which consist of: sar, sadf, mpstat, iostat, nfsiostat, cifsiostat, pidstat and sa tools.

    I will talk about the iostat tool in this article and follow up about some of the other tools.

    On Ubuntu or debian systems you can install this toolset with:

    sudo apt-get install sysstat

    Then you will have access to the iostat command. The command output from executing with no options is below:

    # iostat
    Linux 2.6.32-33-generic (sumo)  09/01/2011      _i686_  (2 CPU)
    
    avg-cpu:  %user   %nice %system %iowait  %steal   %idle
               0.11    0.00    0.47    0.01    0.00   99.41
    
    Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
    sda               0.54         1.85         8.91    1611385    7740024

    This displays the avg-cpu utilization and a device usage list with basic utilization.
    Some options you can use are

    Option Explanation
    -c Display CPU utilization only
    -d Display Device utilization only
    -k Display information in kilobytes
    -m Display information in megabytes
    -t Display time for each report
    -x Display extended statistics

    One more way to use the iostat tool is to specify an interval and count to the command

    iostat -d -x 10 5
    

    This will display the output 5 more times with 10 seconds between each run.

    1 Comment
  • VI: Paste code without formatting

    I happened to be creating a script and found myself trying to copy and past some functions between them. Well my functions
    had some comments and this caused the paste operation into vi to add comments to every line after the first comment.

    So this:

    sub somefunc {
      # input args
      my $var1 = shift;
      my $var2 = shift;
    
      if ($var1 < $var2)
      {
        return $var1;
      }
      else
      {
        return $var2;
      }
    }
    

    Became this:

    sub somefunc {
      # input args
      #my $var1 = shift;
      #my $var2 = shift;
      #
      #if ($var1 < $var2)
      #{
      #  return $var1;
      #}
      #else
      #{
      #  return $var2;
      #}
      #}
    

    This made all my formatting go awry as well, and there were about 4 different functions I was about to cut and past. So I looked into how to make vi accept the text as is without adding formatting. The answer is the paste and nopaste mode.

    To enter the mode to allow pasting without formatting:

    :set paste
    

    To return to regular mode:

    :set nopaste
    

    There are also ways to bind a key to more quickly enter and exit this mode if you are pasting on a regular basis.
    That will be in another article.

    0 Comments
  • Cron Job entry format

    If you ever need to schedule a job regularly on a Unix system, they have a great utility called cron. This will run a job at a regularly scheduled time.

    Here is the format:

    * * * * *  command to be executed
    _ _ _ _ _ 
    | | | | |
    | | | | +--- day of week (0 = Sunday - 6 = Saturday)
    | | | +----- month (1 - 12)
    | | +------- day of month (1-31)
    | +--------- hour(0-23)
    +----------- minute (0-59)
    

    example of a script that runs every day at 7:

    * 7 * * * sendmeupdate
    
    0 Comments
  • How to exit python script

    When I was trying to terminate a python script after a failure, I tried to find the nicest way to exit. The sys.exit() command provided by importing the sys library was what I found.

    The function can take an optional numeric argument, usually in the range of 0-127, and with no argument it returns 0. The return code is passed back to the Operating System.

    import sys
    sys.exit(1)
    

    When exit returns a non-zero value, it is considered an error.

    0 Comments
  • Vi: toggle syntax highlighting

    If you have ever used vi to edit source code, there are occasions where you get on a system that has a very difficult to read color mode. At these times you want the colors to just go away. You can use the following in command mode in vi:

    This will toggle off the syntax highlighting mode.

    :syntax off
    

    This will turn it back on.

    :syntax on
    
    0 Comments
  • Bash: test if file exists

    When you need to do some processing in bash and display if a file exists, the following is a simple script that takes the filename in the $testfile variable and reports if it exists or not.

    if [ -f $testfile ]
    then
      echo $testfile exists!
    else
      echo $testfile does not exist
    fi
    
    0 Comments
  • VI: Display tabs and end of lines

    I was working on a system the other day and needed to see where the end of lines were and where there were tabs instead of spaces. And I found the list option to vi.

    This will turn on the display of “$” to indicate the end of line and “^I” to indicate a tab.

    :set list
    

    This will turn off the option to display the end of line and tabs.

    :set nolist
    
    0 Comments
  • Linux:Script to find files

    I regularly have to find where a file is located and usually use find:

    find . | grep 
    

    Where is replaced with the filename or partial name to match. So I finally decided to write a script to just run this and allow me to print the matching names and even potentially pass the -i flag to grep.

    #!/bin/sh
    
    
    if [ $# -lt 1 ]; then
      echo 1>&2 Usage: $0 ""
      exit 127
    fi
    icase=
    
    while [ $# -ge 1 ]; do
       case $1 in
         -i)  icase=$1;;
          *)  search=$1 ;;
       esac
       shift
    done
    
    find .  | grep $icase $search
    

    This is just a simple script which I have named ffind that searches starting from the current directory. This lets me search with the following commands:

    ffind bak$
    

    This will search for all files that end with “bak” .

    ffind -i edr
    

    This will search for all files with “edr” regardless of case

    0 Comments