繁体   English   中英

Bash Shell编程将变量从文本文件存储到数组中

[英]Bash Shell Programming Store Variables from Text File into Arrays

我的程序应该可以这样工作。

以下是名为BookDB.txt的文本文件的内容。个人之间用冒号(:)分隔,文本文件中的每一行都应作为一组信息,并按以下顺序排列。

标题:作者:价格:数量可用:数量已售

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

我实际上打算1)逐行读取文件并将其存储在数组中2)显示它

但是,我什至不知道如何开始第一个。 从在线研究开始,下面是我迄今为止编写的代码。

#!/bin/bash

function fnReadFile()
{
while read inputline
do
 bTitle="$(echo $inputline | cut -d: -f1)"
 bAuthor="$(echo $inputline | cut -d: -f2)"
 bPrice="$(echo $inputline | cut -d: -f3)"
 bQtyAvail="$(echo $inputline | cut -d: -f4)"
 bQtySold="$(echo $inputline | cut -d: -f5)"
 bookArray[Count]=('$bTitle', '$bAuthor', '$bPrice', '$bQtyAvail', '$bQtySold')
Count = Count + 1
done
}

function fnInventorySummaryReport()
{
fnReadFile
echo "Title                  Author           Price          Qty Avail.      Qty Sold      Total Sales"
for t in "${bookArray[@]}"
do
echo $t
done
echo "Done!"
}

if ! [ -f BookDB.txt ] ; then #check existance of bookdb file, create the file if not exist else continue
touch BookDB.txt
fi

"HERE IT WILL THEN BE THE MENU AND CALLING OF THE FUNCTION"

感谢那些提前提供帮助的人!

您为什么要将整个内容读入数组? 需要信息时查询文件:

#!/bin/sh

# untested code:

# print the values of any line that match the pattern given in $1

grep "$1" BookDB.txt |
while IFS=: read  Title Author Price QtyAvailable QtySold; do
  echo title = $Title
  echo author = $Author
done

除非文本文件很大,否则不太可能需要数组中的数据。 如果由于性能原因它足够大,那么您确实不应该在sh中对此进行编码。

既然您的目标似乎很明确,那么使用awk替代使用bash数组又如何呢? 通常使用正确的工具进行工作会使事情变得容易得多!

以下awk脚本应为您提供所需的内容:

# This will print your headers, formatted the way you had above, but without
# the need for explicit spaces.
BEGIN {
    printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price",
    "Qty Avail.", "Qty Sold", "Total Sales"
}

# This is described below, and runs for every record (line) of input
{
    printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n",
    substr($1, 1, 22), substr($2, 1, 16), $3, $4, $5, ($3 * $5)
}

代码的第二部分(大括号之间)针对输入的每一行运行。 printf用于格式化输出,并使用给定的格式字符串打印出每个字段,用$1$2等表示。在awk ,这些变量用于访问记录的字段(在这种情况下为line)。 substr()用于截断输出,如下所示,但是如果您不介意字段不对齐,可以很容易地将其删除。 我假设“总销售额”应该是“价格”乘以“售出数量”,但是您也可以轻松地进行更新。

然后,将此文件保存在books.awk中, books.awk调用此脚本:

$ awk -F: -f books.awk books
Title                  Author           Price          Qty Avail.      Qty Sold      Total Sales
Harry Potter - The Hal J.K Rowling      40.30          10              50            2015.00
The little Red Riding  Dan Lin          40.80          20              10            408.00
Harry Potter - The Pho J.K Rowling      50.00          30              20            1000.00
Harry Potter - The Dea Dan Lin          55.00          33              790           43450.00
Little Prince          The Prince       15.00          188             9             135.00
Lord of The Ring       Johnny Dept      56.80          100             38            2158.40

-F:告诉awk的字段由冒号分隔( : )和-f books.awk告诉awk来执行这些脚本。 您的数据保存在books

不完全是您的要求,只是为您提供了一种(IMO)更好的工具来进行此类工作! awk可能会令人生畏,但对于这样的记录工作,这真是太神奇了!

暂无
暂无

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

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