2 个变量的乘法

发布于 2024-12-29 00:11:42 字数 969 浏览 1 评论 0原文

假设我正在我的程序中研究我的选择之一。 我有一个文本文件,其中包含以下内容。

格式为 Title:Author:Price:QtyAvailable:QtySold,例如:

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38

下面是我的函数:

printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price","Qty Avail.", "Qty Sold", "Total Sales"
grep "$1" BookDB.txt | while IFS=: read Title Author Price QtyAvailable QtySold; do
  printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold"
done

问题是我需要另一列称为“总销售额”的列,通过乘以 Price 计算得出> 和售出数量。但是,我尝试了不同的方法,例如 $Price * $QtySold$3 * $5 但程序仍然无法为我计算出来。解决这个问题的正确方法或途径应该是什么?

Supposingly I am working on one of my options in my program.
I have a text file which has the below content.

The format is Title:Author:Price:QtyAvailable:QtySold, e.g.:

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38

Below is my function:

printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price","Qty Avail.", "Qty Sold", "Total Sales"
grep "$1" BookDB.txt | while IFS=: read Title Author Price QtyAvailable QtySold; do
  printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold"
done

The problem is I need another column which is called Total Sales, computed by multiplying Price and QtySold. However I have tried different ways such as $Price * $QtySold or $3 * $5 but still the program does not compute it out for me. Which should be the correct method or way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2025-01-05 00:11:42

使用 (())

示例:

A=5 B=6 echo  $(($A*$B))
30

对于浮点数,您应该使用 awk 或bc:

A=5.5 B=6; echo $A \* $B | bc
A=5.5 B=6;  echo -e "$A\t$B" |  awk '{print $1 * $2}'

但是,对整个脚本使用 awk 更能满足您的需求:

awk -F: 'BEGIN{ printf "%-50s %-16s %-14s %-15s %-13s %s\n",
           "Title", "Author", "Price", "Qty Avail.", "Qty Sold", "Total Sales"}
         $1 ~ search  {printf "%-50s %-16s %12.2f   %13d   %10d  %10.2f\n",
            $1, $2, $3, $4, $5, $3 * $5}' BookDB.txt search=Harry

或者如果您希望使用 perl,它会更短一些:

perl -an -F: -s -e 'BEGIN{ printf "%-50s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price", "Qty Avail.", "Qty Sold", "Total Sales"}' \ 
 -e 'printf "%-50s %-16s %12.2f   %13d   %10d  %10.2f\n",@F,$F[2]*$F[4] if /$search/' -- -search=Harry BookDB.txt

use (())

Example:

A=5 B=6 echo  $(($A*$B))
30

For floating point numbers you should use awk or bc:

A=5.5 B=6; echo $A \* $B | bc
A=5.5 B=6;  echo -e "$A\t$B" |  awk '{print $1 * $2}'

However, using awk for the entire script is better for your needs:

awk -F: 'BEGIN{ printf "%-50s %-16s %-14s %-15s %-13s %s\n",
           "Title", "Author", "Price", "Qty Avail.", "Qty Sold", "Total Sales"}
         $1 ~ search  {printf "%-50s %-16s %12.2f   %13d   %10d  %10.2f\n",
            $1, $2, $3, $4, $5, $3 * $5}' BookDB.txt search=Harry

Or if you wish perl, which is a bit shorter:

perl -an -F: -s -e 'BEGIN{ printf "%-50s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price", "Qty Avail.", "Qty Sold", "Total Sales"}' \ 
 -e 'printf "%-50s %-16s %12.2f   %13d   %10d  %10.2f\n",@F,$F[2]*$F[4] if /$search/' -- -search=Harry BookDB.txt
缱倦旧时光 2025-01-05 00:11:42

line=指环王:Johnny Dept:56.80:100:38

echo $line | awk -F":" '{$6=$3*$5; print "$3*$5="$6}'

输出:
$3*$5=2158.4

line=Lord of The Ring:Johnny Dept:56.80:100:38

echo $line | awk -F":" '{$6=$3*$5; print "$3*$5="$6}'

output:
$3*$5=2158.4

蒲公英的约定 2025-01-05 00:11:42

你可以像这样使用 bc :

printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold" "$(echo $QtySold*$Price | bc)"

You could use bc like this:

printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold" "$(echo $QtySold*$Price | bc)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文