how to add up a total in a summary report?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add up a total in a summary report?
# 1  
Old 04-22-2009
how to add up a total in a summary report?

Hi all i got a script up but i cant add up the summary report.. keep having synax error . mind helping me to take a look and tell me what went wrong..

i know is a bit long but i hope someone can hep me with it. thanks

the error message come up when i try to run the sumary report..

i guess 1 of the problem is the system date.thing.

the problem is about point of sales.

i am trying to the add up all the daily sales and then have a grand total of all the sales of all days.
below is the code.




Code:
# Global variables
POS=~/.Point_Of_Sales
POS=POS.txt
export POS

confirm()
{
  echo -en "$@"
  read ans
  ans=`echo $ans | tr '[a-z]' '[A-Z]'`
  if [ "$ans" == "Y" ]; then
    return 0
  else
    return 1
  fi
}

num_lines()
{
  grep -i "$@" $POS|wc -l| awk '{ print $1 }'
}

find_lines()
{
  # Find lines matching $1
  res=-1
  if [ ! -z "$1" ]; then
    grep -i "$@" $POS
    res=$?
  fi
  return $res
}

list_items()
{
  # List items matching given search criteria
  if [ "$#" -eq "0" ]; then
    echo -en "Search for: (return to list all) "
    read search
    if [ -z "$search" ]; then
      search="."
    fi
    echo
  else
    search="$@"
  fi
  find_lines "${search}" | while read i
  do
    echo "$i" | tr ':' '\t'
  done
  echo -en "Matches found: "
  num_lines "$search"
}

add_item()
{
  echo "Add Item: You will be prompted for 3 items:"
  echo "- Item Description, Unit Price, Quantity Purchased -"
  echo
  echo -en "Item Description:"
  read product
  echo -en "Unit Price:"
  read Unit_Price
  echo -en "Quantity:"
  read Quantity
  Date_Purchased=$(date +"%d %b %Y")  
  

  echo "${product}:${Unit_Price}:${Quantity}:${Date_Purchased}" >> $POS
}

locate_single_item()
{
  echo -en "Item to search for: "
  read search
  n=`num_lines "$search"`
  if [ -z "$n" ]; then
    n=0
  fi
  while [ "${n}" -ne "1" ]; do
    #list_items "$search"
    echo -en "${n} matches found. Please choose a "
    case "$n" in 
      "0") echo "less" ;;
      "*") echo "more" ;;
    esac
    echo "specific search term (q to return to menu): "
    read search
    if [ "$search" == "q" ]; then
      return 0
    fi
    n=`num_lines "$search"`
  done
  return `grep -in $search $POS |cut -d":" -f1`
}

remove_item()
{
  locate_single_item
  search=`head -$? $POS | tail -1|tr ' ' '.'`
  if [ -z "${search}" ]; then
    return
  fi
  list_items "$search"
  confirm "Remove?"
  if [ "$?" -eq "0" ]; then
    grep -v "$search" $POS > ${POS}.tmp ; mv ${POS}.tmp ${POS}
  else
    echo "NOT REMOVING"
  fi
}

edit_item()
{
  locate_single_item
  search=`head -$? $POS | tail -1|tr ' ' '.'`
  if [ -z "${search}" ]; then
    return
  fi
  list_items "$search"
  thisline=`grep -i "$search" $POS`
  oldProduct=`echo $thisline|cut -d":" -f1`
  oldUnitPrice=`echo $thisline|cut -d":" -f2`
  oldQuanity=`echo $thisline|cut -d":" -f3`
  echo "SEARCH : $search"
  grep -v "$search" $POS > ${POS}.tmp ; mv ${POS}.tmp ${POS}
  echo -en "Item Description: [ $oldProduct ] "
  read Product
  echo -en "Unit Price[ $oldUnitPrice ] "
  read Unit_Price
  echo -en "Quanity [ $oldQuanity ] "
  read Quanity
  Date_Purchased=$(date +"%d %b %Y")  
  echo "${Product}:${Unit_Price}:${Quanity}:${Date_Purchased}" >> $POS
}
summary_items()
{
  #echo "- - - POS Summary Report - - -"
  #echo "---------------------------"
  #echo "Date       Total POS"
  #echo "---------------------------"
  print_dtotal
  #echo "---------------------------"
  #print_gtotal
  #echo "---------------------------"
  #echo -en "Best Selling Product: "
  #print_bestsell
  #echo -en "Product with Highest POS: "
  #print_phighPOS
  #echo -en "Date with Highest POS: "
  print_dtotal | sort -f4| while read z
    do
        echo $z | cut -f4 -d' '
        return 0
    done
  echo
  echo -en "Press Enter to continue..."
  read
}
print_dtotal()
{
  count=0
  grep -i : $POS | cut -d: -f4 | sort -u | while read i
    do
        dtotal=0
    grep -i "$i" $POS | while read x
        do
            count=`expr $count + 1`
            temp1=`echo $x | cut -d: -f2 | cut -c2-`
            temp2=`echo $x | cut -d: -f3`
            temp3=`calcu_m $temp1 $temp2`
                        dtotal=`calcu_a $dtotal $temp3`
            n=`num_lines "$i"`
               if [ "${n}" -eq "${count}" ]; then
                echo  $i"        $"$dtotal
              fi
        done
    done 

}
print_gtotal()
{
  gtotal=0
  grep -i : $POS | while read x
    do
        temp1=`echo $x | cut -d: -f2 | cut -c2-`
        temp2=`echo $x | cut -d: -f3`
        temp3=`calcu_m $temp1 $temp2`
                gtotal=`calcu_a $gtotal $temp3`
        n=`num_lines "$i"`
           if [ "${n}" -eq "${count}" ]; then
            echo  "Grand Total      $"$gtotal
          fi
        count=`expr $count + 1`
    done 

}
print_bestsell()
{
   grep -i : $POS | sort -k4 -r -t: | while read bs
    do
           echo $bs | cut -d: -f1
        return 0
    done
}
print_phighPOS()
{
      count=0
        phigh="0.0"
    grep -i : $POS | while read x
        do
            count=`expr $count + 1`
            temp1=`echo $x | cut -d: -f2 | cut -c2-`
            temp2=`echo $x | cut -d: -f3`
            temp3=`calcu_m $temp1 $temp2`
            comparex $temp3 $phigh
               if [ "$?" -eq "0" ]; then
                phigh=$temp3
                phighx=`echo $x | cut -d: -f1`
              fi
            n=`num_lines ":"`
               if [ "${n}" -eq "${count}" ]; then
                echo  $phighx
              fi
        done
}
comparex()        # compare 2 decimal numbers $1 $2 eg 1.20 and 3.60
{
    x1=`echo $1 | cut -d. -f1`
    x2=`echo $1 | cut -d. -f2`
    y1=`echo $2 | cut -d. -f1`
    y2=`echo $2 | cut -d. -f2`
    if [ "${x1}" -gt "${y1}" ]; then
        return 0
    elif [ "${x1}" -eq "${y1}" ]; then
        if [ "${x2}" -gt "${y2}" ]; then
            return 0
        fi
    fi
    return 1
}
calcu_a()        # add 2 decimal numbers $1 $2
{
    x1=`echo $1 | cut -d. -f1`
    x2=`echo $1 | cut -d. -f2`
    y1=`echo $2 | cut -d. -f1`
    y2=`echo $2 | cut -d. -f2`
    x1=`expr $x1 + $y1`
    x2=`expr $x2 + $y2`
    x3=`expr $x2 \/ 100`
    x1=`expr $x1 + $x3`
    x2=`expr $x2 \% 100`
    echo $x1"."$x2
}
calcu_m()        # multiply 2 decimal numbers $1 $2
{
    x1=`echo $1 | cut -d. -f1`
    x2=`echo $1 | cut -d. -f2`
    y=`echo $2`
    x1=`expr $x1 \* $y`
    x2=`expr $x2 \* $y`
    x3=`expr $x2 \/ 100`
    x1=`expr $x1 + $x3`
    x2=`expr $x2 \% 100`
    echo $x1"."$x2
}

# 2  
Old 04-22-2009
What happens when you move function print_dtotal() above summary_items()?
# 3  
Old 04-22-2009
Hi all , below i am suppose to key in and enter the daily sales in to the system.

and i am suppose to get the daily total sales and the grand total sales when i ask for summary listing
below is the code that i have write.. can any one help?

the function doesnt seems to bea ble to reconise the system day and promt me a syntaxerror..



Code:
#Enter and add in the Item detail, Item Price and Quantity Purchased into the system.
add_item()
{
  echo "Add Item: You will be prompted for 3 items:"
  echo "- Item Description, Unit Price, Quantity Purchased -"
  echo
  echo -en "Item Description:"
  read product
  echo -en "Unit Price:"
  read Unit_Price
  echo -en "Quantity:"
  read Quantity
  Date_Purchased=$(date +"%d %b %Y")  
  
  echo "${product}:${Unit_Price}:${Quantity}:${Date_Purchased}" >> $POS
}
#i am printing out all the daily sales in a day   print_dtotal
#i am printing out all the grand total sales of all days  print_gtotal
summary_items()
{
  
  print_dtotal
 
  print_gtotal
  
  print_dtotal | sort -f4| while read z
    do
        echo $z | cut -f4 -d' '
        return 0
    done
  echo
  echo -en "Press Enter to continue..."
  read
}
print_dtotal()
{
  count=0
  grep -i : $POS | cut -d: -f4 | sort -u | while read i
    do
        dtotal=0
    grep -i "$i" $POS | while read x
        do
            count=`expr $count + 1`
            temp1=`echo $x | cut -d: -f2 | cut -c2-`
            temp2=`echo $x | cut -d: -f3`
            temp3=`calcu_m $temp1 $temp2`
                        dtotal=`calcu_a $dtotal $temp3`
            n=`num_lines "$i"`
               if [ "${n}" -eq "${count}" ]; then
                echo  $i"        $"$dtotal
              fi
        done
    done 
}
print_gtotal()
{
  gtotal=0
  grep -i : $POS | while read x
    do
        temp1=`echo $x | cut -d: -f2 | cut -c2-`
        temp2=`echo $x | cut -d: -f3`
        temp3=`calcu_m $temp1 $temp2`
                gtotal=`calcu_a $gtotal $temp3`
        n=`num_lines "$i"`
           if [ "${n}" -eq "${count}" ]; then
            echo  "Grand Total      $"$gtotal
          fi
        count=`expr $count + 1`
    done 
}
calcu_a()        # add 2 decimal numbers $1 $2
{
    x1=`echo $1 | cut -d. -f1`
    x2=`echo $1 | cut -d. -f2`
    y1=`echo $2 | cut -d. -f1`
    y2=`echo $2 | cut -d. -f2`
    x1=`expr $x1 + $y1`
    x2=`expr $x2 + $y2`
    x3=`expr $x2 \/ 100`
    x1=`expr $x1 + $x3`
    x2=`expr $x2 \% 100`
    echo $x1"."$x2
}
calcu_m()        # multiply 2 decimal numbers $1 $2
{
    x1=`echo $1 | cut -d. -f1`
    x2=`echo $1 | cut -d. -f2`
    y=`echo $2`
    x1=`expr $x1 \* $y`
    x2=`expr $x2 \* $y`
    x3=`expr $x2 \/ 100`
    x1=`expr $x1 + $x3`
    x2=`expr $x2 \% 100`
    echo $x1"."$x2
}

# 4  
Old 04-22-2009
So all other functions run fine except the summary_items() function? I'm not clear on what error it is you're seeing.
# 5  
Old 04-22-2009
Quote:
Originally Posted by xiaojesus
Hi all i got a script up but i cant add up the summary report.. keep having synax error . mind helping me to take a look and tell me what went wrong..

i know is a bit long but i hope someone can hep me with it. thanks

the error message come up when i try to run the sumary report..

What error do you get?

Is it: sort: invalid option -- 4
Quote:

i guess 1 of the problem is the system date.thing.

the problem is about point of sales.

i am trying to the add up all the daily sales and then have a grand total of all the sales of all days.
below is the code.


Code:
# Global variables
POS=~/.Point_Of_Sales
POS=POS.txt


You are assigning two different values to POS; only the last assignment endures; the first might as well never have been made.
Quote:
Code:
export POS

confirm()
{
  echo -en "$@"
  read ans
  ans=`echo $ans | tr '[a-z]' '[A-Z]'`
  if [ "$ans" == "Y" ]; then
    return 0
  else
    return 1
  fi
}


Avoid options to echo; they are non-standard as is ==.

You do not need to call tr; you can check the answer without it:

Code:
confirm()
{
  printf "%s " "$*"
  read ans
  case "$ans" in
    [Yy]) true ;;
    *) false ;;
  esac
}

Quote:
Code:
num_lines()
{
  grep -i "$@" $POS|wc -l| awk '{ print $1 }'
}


That will fail if there is more than one argument.

There's no need for awk or wc:

Code:
  grep -ni "$1" "$POS"

Quote:
Code:
find_lines()
{
  # Find lines matching $1
  res=-1
  if [ ! -z "$1" ]; then
    grep -i "$@" $POS
    res=$?
  fi
  return $res
}

Code:
find_lines()
{
  # Find lines matching $1
  if [ -n "$1" ]; then
    grep -i "$1" "$POS"
  fi
}

Quote:
Code:
...
locate_single_item()
{
  echo -en "Item to search for: "
  read search
  n=`num_lines "$search"`
  if [ -z "$n" ]; then
    n=0
  fi
  while [ "${n}" -ne "1" ]; do
    #list_items "$search"
    echo -en "${n} matches found. Please choose a "
    case "$n" in 
      "0") echo "less" ;;
      "*") echo "more" ;;
    esac
    echo "specific search term (q to return to menu): "
    read search
    if [ "$search" == "q" ]; then
      return 0
    fi
    n=`num_lines "$search"`
  done
  return `grep -in $search $POS |cut -d":" -f1`


There's no need for cut:

Code:
return `grep -hin $search $POS`

Quote:
Code:
}

...
summary_items()
{
  #echo "- - - POS Summary Report - - -"
  #echo "---------------------------"
  #echo "Date       Total POS"
  #echo "---------------------------"
  print_dtotal
  #echo "---------------------------"
  #print_gtotal
  #echo "---------------------------"
  #echo -en "Best Selling Product: "
  #print_bestsell
  #echo -en "Product with Highest POS: "
  #print_phighPOS
  #echo -en "Date with Highest POS: "
  print_dtotal | sort -f4| while read z


Code:
sort: invalid option -- 4

Quote:
Code:
    do
        echo $z | cut -f4 -d' '
        return 0
    done
  echo
  echo -en "Press Enter to continue..."
  read
}
...

# 6  
Old 04-22-2009
ya all the other function is working fine except the summary part.. any one can help me with it?
# 7  
Old 04-22-2009

No, not if you don't answer the questions you have been asked.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate a Summary report

Hi All, Script to meet my requirement might be simpler for UINIX experts.. :) I need to generate an summary report in .txt file using shell script I have Reject directory in Unix server which contains all reject files for three diff categories- Presentation, Chapter and Scene Following... (3 Replies)
Discussion started by: Sakthikalluri
3 Replies

2. Shell Programming and Scripting

Summary report csv file

Hello, I have 2 csv files with 4 columns each. file1.csv A, AA, AAA, AAAA B, BB, BBB, BBBB file2.csv C, CC, CCC, CCCC D, DD, DDD, DDDD I would like to use shell commands (sed, awk...) to copy the content of the 2 files (2x4 columns) into a final csv template file. Expected... (2 Replies)
Discussion started by: inMyZone35
2 Replies

3. Linux

How to calculate total CPU usage from SAR report?

Hi, I want to calculate the total cpu usage from the sar report. Say for example, Linux 2.6.24-21-generic (blade10) 09/10/2012 04:54:36 PM CPU %user %nice %system %iowait %steal %idle 04:54:37 PM all 0.00 0.00 0.00 0.00 ... (1 Reply)
Discussion started by: rohitmd
1 Replies

4. Shell Programming and Scripting

Parse diff output into very detailed & summary report

Hello all; I'll try an explain my dilemma as best I can. But first some background: 1- I am suppose to compare a database to itself before and after changes; basically generate audit trail report. 2- This database contains "RULES" (the id field) that we use for transmitting files. 3 - The... (0 Replies)
Discussion started by: gvolpini
0 Replies

5. UNIX for Dummies Questions & Answers

Print summary or the total disk usage of conf file

hey i want to print the summary or the total disk usage of the configuration files that are in the /etc directory printed in human-readable format. i think i got somewhere right as am using wc *.conf commands but i am unsure how to use to put it in human-readable format with the wc command. ... (13 Replies)
Discussion started by: stefanere2k9
13 Replies
Login or Register to Ask a Question