简体   繁体   English

Bash从文本文件计算出打印输出

[英]Bash to calculate print out from text file

The script I am trying to create will have two functions. 我尝试创建的脚本将具有两个功能。 However I am facing a some error and appreciate if someone can guide me. 但是,我遇到了一些错误,如果有人可以指导我,我将不胜感激。

  1. To calculate profit using the field based on "Price" and "Quantity Sold". 要使用基于“价格”和“售出数量”的字段来计算利润。
  2. Print out everything inside the text file. 打印出文本文件中的所有内容。

The format of the data inside my BookDB text file is shown below. 我的BookDB文本文件中的数据格式如下所示。 The text file contain only 5 field and the 6th field which refer to profit should only be shown and not saved into the text file. 文本文件仅包含5个字段,而涉及利润的第6个字段应仅显示而不应保存到文本文件中。 The delimiter in my database is ":". 我的数据库中的定界符是“:”。

Title:Author:Price:Quantity avilable:Quantity Sold
a(Title):b(Author):$1.00(Quantity.avilable):52:248(Quantity Sold)

Below is the desired output 以下是所需的输出

Title:Author:Price:Quantity Available:Quantity Sold:Profit
A:B:1.00:20:10:$10

Right now, my output is like this 现在,我的输出是这样的

Advanced Book Inventory System
    1.) Inventory summary report
    2.) Quit
Enter selection: 1
6) inventory_summary_report
./filesearch.sh: line 17: (40.30

40.80
50.00
55.00
15.00
56.80
89.10
76.00
23.60
12.99
1.00)*(50
10
248): command not found
./filesearch.sh: line 18: BEGIN: command not found
Harry Potter - The Half Blood Prince,J.K Rowling,$40.30,10,50,Harry Potter - The Half 
Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood,Dan Lin,$40.80,20,10,The little Red Riding Hood:
Dan Lin:40.80:20:10

Below is my coding, appreciate if anyone can tell me my mistake in the coding 以下是我的编码,如果有人能告诉我我的编码错误,请多谢

#!/bin/bash
clear
function press_enter
{
 echo ""
 echo -n "Press Enter to continue"
 read
 clear
}

  function inventory_summary_report
   {
    echo "1) inventory_summary_report"
    echo ""
    price="$(cat BookDB.txt | cut -d: -f3)"
    sold="$(cat BookDB.txt | cut -d: -f5)"
    sales=$(echo scale=3;"($price)*($sold)" |bc)
    BEGIN { printf "%s,%s,$%s,%s,%s,%s\n" , "Title", "Author", "Price", "Qty Avail", "Qty  Sold", "Total Sales" }
    awk -F':' -v search="$1" '$1 ~ search || $2 ~ search { i++; printf  "%s,%s,$%s,%s,%s,%s\n", $1, $2, $3, $4, $5,$sales} END { printf "%d records found\n", i }'     BookDB.txt
   }

  #Display menu options and wait for selection
    selection=0
    until [ "$selection" = "1" ]; do
     echo ""
     echo ""
     echo "Advanced Book Inventory System"
     echo ""
     echo ""
     echo " 1.) Inventory summary report"
     echo " 2.) Quit"

  echo -n "Enter selection: "
  read selection
   echo ""
   case $selection in
     1 ) inventory_summary_report;press_enter;;
     2 ) break ;;
     * ) tput setf 4;echo "Please enter 1 or 2, ";tput setf 7; press_enter
 esac
 done`

Here's a gift. 这是礼物 So far this is what I can suggest. 到目前为止,这是我可以建议的。

#!/bin/bash

clear

function press_enter {
    echo ""
    echo -n "Press Enter to continue. "
    read
    clear
}

function inventory_summary_report {
    awk -F : -v search="$1" '
        BEGIN {
            OFS = "\t"
            print "Title", "Author", "Price", "Qty Avail", "Qty  Sold", "Total Sales"
            count = 0
        }
        $1 ~ search {
            printf "%s\t%s\t$%0.2f\t%s\t%s\t%0.2f\n", $1, $2, $3, $4, $5, ($3 * $5)
            ++count
        }
        END {
            printf "%d records found\n", count
        }
    ' BookDB.txt | column -t  ## Better than relying on constant column sizes with printf.
}

# Display menu options and wait for selection

selection=0

for (( ;; )); do
    echo ""
    echo ""
    echo "Advanced Book Inventory System"
    echo ""
    echo ""
    echo " 1.) Inventory summary report"
    echo " 2.) Quit"
    echo ""
    read -p "Enter selection: " selection
    echo ""
    case "$selection" in
    1)
        inventory_summary_report  ## You're not passing any argument for the search. You probably still need to ask input here.
        press_enter
        break  ## Optional.
        ;;
    2)
        break
        ;;
    *)
        tput setf 4
        echo "Please enter 1 or 2, "
        tput setf 7
        press_enter
        ;;
    esac
done

Generally speaking, awk requires > in its syntax for file output. 一般来说,awk在其语法中要求>才能输出文件。 So it may be as easy as: 因此可能很简单:

awk -F':' -v search="$1" '$1 ~ search || $2 ~ search { i++; printf  "%s,%s,$%s,%s,%s,%s\n", $1, $2, $3, $4, $5,$sales} END { printf "%d records found\n", i }' > BookDB.txt, 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM