Friday, July 10, 2015

My Attempt Of Parsing an Information from Log Files in Linux or in z/OS Unix

My first attempt of parsing an information from a huge log files. I have created a unix script just to extract the information from within a range in a Linux or a Unix environment.

1st: I created a file in unix /usr/bin/parse.
  • Command: sudo nano /usr/bin/parse
  • Copy the script below and paste it in the newly create parse file

2nd: Make the /usr/bin/parse executable.
  • Command: sudo chmod ug+rwx,o+rx /usr/bin/parse

Script for parsing:


#!/bin/bash
# parse - will dump or tail a file between range from and to
# regular expression to validate if the input is number
re='^[0-9]+$'
if [ "$#" -eq 0 ]; then
  read -p "Please enter filename : " filename
  # this will check if the input for filename is empty
  if [ -z $filename ]; then
     echo "File name $filename must not be empty. Please try again"; exit 1
  fi
  # this will check if the file exist
  if [ -r $filename ]; then
     # Get the input for start range
     read -p "What line would you like to start : " startno

     if ! [[ $startno =~ $re ]] ; then
        echo "error: Start number is an invalid input" >&2; exit 1
     fi

     read -p "What line would you like to end   : " endno
     if ! [[ $endno =~ $re ]] ; then
        echo "error: End number is an invalid input" >&2; exit 1
     fi
     eval "sed -n '$startno,$endno p' $filename"
  else
     echo "File name $filename does not exist"
     exit 1
  fi
elif [ "$#" -eq 2 ]; then
  echo "$1"
  echo "$2"
  echo "awk 'NR>=$1' $2"
  eval "awk 'NR>=$1' $2"
elif [ "$#" -eq 3 ]; then
  echo "$1"
  echo "$2"
  echo "$3"
  echo "sed -n '$1,$2p' $3"
  eval "sed -n '$1,$2p' $3"
else
 echo -n "Invalid Argument. Example: parse 100 1000 myfiles.log"
fi