eTechtips

Tech Tips and Insights

Category: Utilities

  • VI: Reload Current file

    When I am editing and having a “clumsy fingers” day, I find that I have to reload the file that I am editing from disk repeatedly. I usually just do a quick “:q!” and re-run the vi command that opened the file. But there is a better way!

    Use the :edit command:

    :edit
    

    Or you can shorten it to

    :e
    

    Now if you have typed in the window you are editing and want to reload from disk anyway without saving, remember to add the exclamation point(!) to the command.

    :edit!
    

    or

    :e!
    
    0 Comments
  • CSS: Background properties

    These are the individual background properties in CSS.

    background-color
    background-image
    background-repeat
    background-attachement
    background-position
    

    The background-color property specifies the color of the element. This can be useful to specify if someone has turned images off and you want a non-default view of your content. You can use a hex value (#RRGGBB), a shortcut hex value (#RGB) or a named color (red, green,blue).

    background-color: #fffffff;
    

    The background-image specifies an image to place in the background of an element. Remember that the image should be supported by as many browsers as possible. This means using jpeg, pngs or gifs, not tifs or proprietary image formats.

    background-image: url(images/someimage.gif)
    

    The background-repeat property specifies how an image should display within an element. You can have it repeat across (repeat-x), repeat downwards (repeat-y) or not repeat (no-repeat).

    background-repeat: repeat-x;
    

    The background-attachmemnt attribute is used to specify if an image should scroll (scroll) with the page, be fixed (fixed) to the page or inherit position from a parent element (inherit).

    background-attachment: fixed;
    

    The background-position attribute is used to specify the starting position of an image. This can be useful if you are using a sprite to speed up loading times. The background-position attribute can take a Xpx Ypx argument, an X% Y% argument, named arguments(left top, right bottom,…) or inherit from the parent.

    background-position: 50% 50%;
    

    Here is an example of the use of these:

     body {
       background-color: #ffffff;
       background-image: url(images/someimage.gif);
       background-repeat: no-repeat;
       background-attachment: fixed ;
       background-position: left top ;
    }
    

    You can also use a short cut method to the background property.

     .imagebox {
      background: #ffffff url(images/someimage.gif) no-repeat fixed right top;
    }
    
    0 Comments
  • Notepad++: Changing shortcut keys

    I was tracking a log file the other day and had to reload the file from disk regularly so I wanted to find out how to make a shortcut key to the Reload from disk menu entry on the File menu. Using the Settings->Shortcut Mapper to update the shortcuts for a given menu entry, I chose the Ctrl-R sequence but you are welcome to find an unmapped key sequence that works for you.

    0 Comments
  • Viewing many different video formats on Windows

    So I lost my hard drive and went to play some of the videos that I have saved on the system and could no longer play them in Windows Media Player. I needed to download a set of video codecs to play my .vob files. I am using the K-Lite Codec Pack.

    Featured Software

    VIDEO CODECS
    FFDShow MPEG-4
    DivX 8
    Koepi’s XviD Codec
    DScaler MPEG Filters
    Ogg Codecs
    Xvid Video Codec
    Nic’s XviD Codec
    Ligos Indeo Codec
    AUDIO CODECS
    MPEG Layer-3 Codec
    AC3 Filter
    AC-3 ACM Codec
    CoreVorbis Decoder
    LAME DirectShow Filter
    LAME MP3 Encoder
    Monkey’s Audio
    Vorbis Ogg ACM Codec

    This allows me to view the files in other video apps as well.

    0 Comments
  • 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