Saturday, October 6, 2012

How to find the Largest and Smallest number in the given numbers in Unix


###############################################################
# To find the Largest and Smallest number in the given numbers      #
###############################################################

#!/bin/ksh

# To make sure sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please provide the sufficient arguments"
  echo "bigsmall file_name"
  exit -1
fi

# Declaring and Assigning values to the local variables
set -A LIST `cat /clocal/fme/prod/forecast/padu/Lilly/numberlist.txt`
a=${#LIST[*]}
Small=${LIST[0]}
Big=${LIST[0]}

# Implementation of logic to find the biggest and smallest number
for i in ${LIST[*]}
do
 if [ $i -lt $Small ]
  then
  Small=$i
 elif [ $i -gt $Big ]
  then
  Big=$i
 fi
done
 echo "Smallest = $Small"
 echo "Biggest = $Big"

exit 0

How to find the biggest among 3 given numbers in Unix


##################################################################
# To find the biggest among 3 given numbers                                          #
##################################################################

#!/bin/ksh/

# To make sure sufficient arguments
if [ $# -ne 3 ]
then
echo "Please provide the three arguments"
exit -1
fi

# Declaring and assigning values to the local variables
A=$1
B=$2
C=$3

#Logical part to find the largest number among 3 numbers
if [ $A -gt $B  -a  $A -gt $C ]
   then
   echo "A $A is the biggest number"
        elif  [ $B -gt $A  -a  $B -gt $C ]
        then
        echo "B $B is the biggest number"
                elif  [ $C -gt $A -a  $C -gt $B ]
                then
                echo "C $C is the biggest number"

   elif  [ $A -eq $B -a $A -eq $C -a $B -eq $C -a  $C -eq $B ]
   then
  echo "All numbers are same"
fi

exit 0

Reverse the content in the file using Unix


#####################################################
# Reverse the content in the file                                       ##
#####################################################

#!/bin/ksh

# To make sure the sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please enter the file name to be reverse"
  exit -1
fi

# Implementation of logic to reverse the content in the file
if [ -f $1 ]
  then
    rever=`rev $1`
 echo "Original file is"
 cat $1
 echo "Reverse of the file is"
 echo $rever
fi

exit 0

How to combine the files Vertically or Horizontally in Unix


########################################################
# Combine the files vertically or Horizontally                        ##
########################################################

#!/bin/ksh

# To make sure sufficient arguments
if [ $# -ne 2 ]
then
  echo "Please provide the two file names to be combine"
  exit -1
fi

# Initializing the local variables
file1=$1
file2=$2
Hori=/clocal/fme/prod/forecast/padu/Lilly/Horizontal.txt
verti=/clocal/fme/prod/forecast/padu/Lilly/Vertical.txt
echo "Please choose the combination format"
echo "1) Horizontal"
echo "2) Vertical"
read ch
case $ch in
1) cat $file1>$Hori
   cat $file2>>$Hori;;
2) paste $file1 $file2>$verti;;
*) ;;
esac
exit 0

How to display the alternative numbers in the given number in UNIX


##########################################################
# To display the alternative numbers in the given number         #
##########################################################

#!/bin/ksh

echo "Please enter one number"
read num
echo $num>temp.txt
i=1
len=`cat temp.txt|wc -c`

#Logic implementation

while [ $i -le $len ]
do
  alseries=`echo $num|cut -c $i`
  echo $alseries
i=`expr $i + 2`
done

rm -f temp.txt

exit 0