eTechtips

Tech Tips and Insights

Category: Utilities

  • grep -e :searching multiple terms

    When you have to process log files, it can be very useful to highlight the terms you are seeking. One way to highlight a term is to use grep with the –color option. The drawback to using this method is that if you are chaining the grep command after another command then the last grep is all that would be highlighted.
    I found that you could use the following to highlist all terms:

    grep --color -e 'search|search2' 
    

    So to break down the command line, first we have the grep command followed by the –color option which (if your terminal allows it) will highlight the search term within the screen results. This means that if you put this into a file, then the color highlighting will be lost. The ‘-e’ option states that the next argument is going to be a regular expression based argument.
    The full syntax of regular expressions is beyond the scope of this article but basically is a pattern to match strings. The reason I am using a -e option is to support the ‘|’ option that allows me to search for more than one term. The reason that the pipe character is escaped is that otherwise it would be considered as a character to search for instead of a separator. This also will color either term within the output. So I am searching for search or search2 anywhere within the files that I pass in.

    0 Comments
  • My Favorite Vi(m) commands

    I have found myself using Vim as my Linux editor of choice.  It is not dependent on X11 being available and I can edit pretty much anything fairly quickly.  I have been using a Redhat flavored linux(Redhat,Fedora, Centos) for quite sometime and because I couldn’t guarantee that Emacs would be installed I just got used to using Vi.

    When I first attempted to use Vi after using Emacs from my college days, I was initially very frustrated with modes and key sequences.  But after a very short time I found I could edit files very rapidly and make changes quite easily.

    Quick key list:

    Esc The escape key is the way to get out of editor mode and clear
    Ctrl-c will exit the editor mode
    :q! Exits without saving
    u Undo
    cw changes the following word
    dd deletes the line
    D deletes the line to the end
    J removes the end of the line
    :e or :edit reload the file from disk

    1) The thing that I do quite often is to replace all Ctrl-M (Windows Returns) within a file.  (Yes, I could do this with a call to dos2unix, but this is a Vi list).   I found the following key sequence works quite well.

    :%s/Ctrl-v<enter>//g

    breaking this sequence down

    2) Another sequence I use quite often is to reformat source code.

    :1 <enter> <shift-v> <shift>-g =

    breaking the sequence down

    :1<enter>   – states go to the first line of the file

    <shift>-v    – Turns on visual line mode which selects the entire current line

    <shift>-g    -Makes the cursor go to the last line in the file and with the last command

    selects the entire file

    =                   – Tells the editor to reformat the selection area

    This page will be updated as I figure out more useful commands. This is only a small amount of the power of VI!

    0 Comments
  • Awk: Separating delimited files on the command line

    I have had to process data files from customers that have come in as delimited files. The files are usually in CSV formats and the delimiters might change depending on the content of the files. Sometimes the delimiters are usually tabs”t”, pipes “|”, comma “,” or semi-colon “;”. A useful tool for processing this type of file is to use awk.

    Example command line:

    awk -F 't' '{OFS="|"; if (NR != 1) print $3,$2,$1}'
    

    The ‘-F’ argument is followed by the character that you have the file separated on. The example above uses a tab escape sequence ‘t’ but you could have any character. One potential problem is if the data contains the same character that you are attempting to split on. As in a comma separated list using commas within a long text sequence.
    The Second sequence of quoted text above starts the processing of the file. The if statement uses the NF keyword which stands for Number of Row. This tells the script that it should not fire for the first row. The print statement allows you to pick and choose the columns(numbered from 1) to use or allows you to reorder them.
    The ‘OFS’ part of the command line that helps with the print statement describes the Output Field Separator. In the case above it is using a pipe character ‘|’ to separate the text as it is being printed out. This allows you comma separate the fields that you want instead of having to use a more sophisticated printf statement.

    The input file is of the form

    Label,name,address
    

    And the output is going to be of the form:

    address,name,Label
    
    0 Comments
  • DOS: Capture return values from commands

    When I am writing build automation scripts, there are times that I have to insure that the previous command completed
    with no errors. I have found that ERRORLEVEL gets filled in with the return value from a command.

    dir %1
    
    if (%ERRORLEVEL%) == (0) echo Found File: %1
    if (%ERRORLEVEL%) == (1) echo Missing File: %1
    

    So if you save the previous in a file and run the command with one (1) argument, it will display the output of the dir command
    but also print either “Found file: ” or “Missing File: “.

    0 Comments
  • Perl: Format date strings

    The other day I had a requirement to fill in a date value while creating an entity in ClearQuest. The field that was required was a date and was formatted as “mm/dd/yyyy hh:MM::SS AM|PM”. I used the localtime function to return the date and used sprintf to format the date to the proper output.
    The reason I used sprintf was that for hours, months and days less 10 the output was 1 digit instead of two and the input was not allowed. The %02d states that the digits used will be a minumum of 2 in this case.

    Here is the sample program I wrote to test the expected output:

     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    
    # formatted just by concatenating output
    my $datestring = $mon . "/" . $mday . "/" . $year . "  " . $hour . ":" . $min.
    ":" . $sec  ;
    
    # formatted using sprintf to ensure proper digit counts are output
    my $datestring2 = sprintf("%02d/%02d/%04d %d:%02d:%02d %s",$mon,$mday,1900 +$yea
    r,$hour,$min,$sec, $hour >= 12? "PM" : "AM");
    
     print $datestring;
      print $datestring2;
    
    
    0 Comments
  • Using Rsync to backup a directory

    Recently I was moving data between an old server and new web server. I had some 250 GB of data and thought about just using Secure Copy (scp). When I started the process, I walked away
    thinking “No Problem”, but after a few files, I got an error due to file permissions and I did not want to figure out what files had not been copied yet or where to try to restart.
    So I used rsync to mirror the drive. I was able to set it up and due to my slow network speed, it still took several days, but I was certain that I had gotten all the data.

    rsync -azuv -e ssh  user@originserver:public_html/* public_html/
    

    The options I used were for :

    Option Description
    -a Archive Mode
    -z Compress File Data
    -u Update Mode
    -v Verbose
    -e specify command to run

    This took some time to run, but I did not have to babysit the command and it duplicated the data from my old server to the new one.

    0 Comments
  • DOS: Check for empty environment variable

    I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line.

    SET PASS=somepassword
    : or
    :SET PASS=
    if x%PASS%= X (
      SET PASSARG=--password somepassword
    ) else
    (
      SET PASSARG=
    )
    
    commandtorun %PASSARG%
    

    Now if the user comments out his password, the script will make the PASSARG environment variable be empty or if they set a password the passarg variable will be filled in.

    0 Comments
  • Java: Command line argument processing

    In each language that I have learned I have found it useful to know how to process command line arguments and here is a simple framework that I use for Java. I usually wrap the java command line in a bat or shell file to make it easier to run so that the user does not have to worry about classpath or path issues.

    The following is the basic code to handle the command line arguments.

    class CmdArgs
    {
    
      public static void main(String[] args)
      {
          int argsize = args.length;
          String curarg;
    
          if (argsize > 0 )
          {
           
          }
          
          for(int i = 0; i < argsize; i++)
          {
              if (args[i].equals("--host"))
              {
                 i++;
                 host = args[i];
               }
              else if (args[i].equals("--debug"))
              {
                   debug = 1;
              }
              else
              {
                  System.out.println("Unknown option: " + args[i]);
                  System.exit(1);
              }
           }
    
       }
    }
    

    The bat file for Dos/Windows might look like this:

    @echo off
    
    java -cp c:javabin;somejar.jar  com.etechtips.CmdArgs %*
    

    The shell based file might look like:

    java -cp /home/ecdown/javabin:somejar.jar com.etechtips.CmdArgs $*
    
    0 Comments
  • Linux:Script to use find to search contents of files

    I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like:

    find . -exec grep "somestring" {} ; --print
    

    This find command will search from the current directory and for each file will run grep on it and if it finds a matching string, it will print the string and then the filename. So I took this and moved it into a script.

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

    This allows me to run a command such as

    ffind -i Super
    

    This command will search all file names starting from the current directory and recurse directories and the “-i” will tell it to ignore case.

    0 Comments
  • Linux: Processor info and speed

    There are times that I get on a Linux box and want to know about some of the resources available to me. There are tools that can accomplish this, but there are some files that contain basic settings about the resources available to the system. One of those file is:

    /proc/cpuinfo
    

    This file contains information about the Processors and you can do a simple:

    more /proc/cpuinfo
    

    to see all the details or you can grep for certain fields:

    processor       -- This will show you how many processor entries there are.
    cpu MHz  or MHz -- This will show you the speed of the processors.
    cache size      -- This will show you the cache size available to the processor.
    model           -- This will show you the type and model of the processor you are using.
    

    There are quite a few more fields, but these are the ones that I check the most.

    0 Comments