簡體   English   中英

Bash從文本文件計算出打印輸出

[英]Bash to calculate print out from text file

我嘗試創建的腳本將具有兩個功能。 但是,我遇到了一些錯誤,如果有人可以指導我,我將不勝感激。

  1. 要使用基於“價格”和“售出數量”的字段來計算利潤。
  2. 打印出文本文件中的所有內容。

我的BookDB文本文件中的數據格式如下所示。 文本文件僅包含5個字段,而涉及利潤的第6個字段應僅顯示而不應保存到文本文件中。 我的數據庫中的定界符是“:”。

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

以下是所需的輸出

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

現在,我的輸出是這樣的

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

以下是我的編碼,如果有人能告訴我我的編碼錯誤,請多謝

#!/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`

這是禮物 到目前為止,這是我可以建議的。

#!/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

一般來說,awk在其語法中要求>才能輸出文件。 因此可能很簡單:

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