[英]Bash Shell Programming Store Variables from Text File into Arrays
My program should be able to work this way. 我的程序应该可以这样工作。
Below is the content of the text file named BookDB.txt The individual are separated by colons(:) and every line in the text file should serve as a set of information and are in the order as stated below. 以下是名为BookDB.txt的文本文件的内容。个人之间用冒号(:)分隔,文本文件中的每一行都应作为一组信息,并按以下顺序排列。
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
I actually intend to 1) Read the file line by line and store it in an array 2) Display it 我实际上打算1)逐行读取文件并将其存储在数组中2)显示它
However I have no idea on how to even start the first one. 但是,我什至不知道如何开始第一个。 From doing research online, below are the codes which I have written up till now. 从在线研究开始,下面是我迄今为止编写的代码。
#!/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"
Thanks to those in advance who helped! 感谢那些提前提供帮助的人!
Why would you want to read the entire thing into an array? 您为什么要将整个内容读入数组? Query the file when you need information: 需要信息时查询文件:
#!/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
Unless your text file is very large, it is unlikely that you will need the data in an array. 除非文本文件很大,否则不太可能需要数组中的数据。 If it is large enough that you need that for performance reasons, you really should not be coding this in sh. 如果由于性能原因它足够大,那么您确实不应该在sh中对此进行编码。
Since your goal here seems to be clear, how about using awk
as an alternative to using bash
arrays? 既然您的目标似乎很明确,那么使用awk
替代使用bash
数组又如何呢? Often using the right tool for the job makes things a lot easier! 通常使用正确的工具进行工作会使事情变得容易得多!
The following awk
script should get you something like what you want: 以下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)
}
The second section of code (between curly braces) runs for every line of input. 代码的第二部分(大括号之间)针对输入的每一行运行。 printf
is for formatted output, and uses the given format string to print out each field, denoted by $1
, $2
, etc. In awk
, these variables are used to access the fields of your record (line, in this case). printf
用于格式化输出,并使用给定的格式字符串打印出每个字段,用$1
, $2
等表示。在awk
,这些变量用于访问记录的字段(在这种情况下为line)。 substr()
is used to truncate the output, as shown below, but can easily be removed if you don't mind the fields not lining up. substr()
用于截断输出,如下所示,但是如果您不介意字段不对齐,可以很容易地将其删除。 I assumed "Total Sales" was supposed to be Price multiplied by Qty Sold, but you can update that easily as well. 我假设“总销售额”应该是“价格”乘以“售出数量”,但是您也可以轻松地进行更新。
Then, you save this file in books.awk
invoke this script like so: 然后,将此文件保存在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
The -F:
tells awk
that the fields are separated by colon ( :
), and -f books.awk
tells awk
what script to run. 该-F:
告诉awk
的字段由冒号分隔( :
)和-f books.awk
告诉awk
来执行这些脚本。 Your data is held in books
. 您的数据保存在books
。
Not exactly what you were asking for, but just pointing you toward a (IMO) better tool for this kind of job! 不完全是您的要求,只是为您提供了一种(IMO)更好的工具来进行此类工作! awk
can be intimidating at first, but it's amazing for jobs that work on records like this! awk
可能会令人生畏,但对于这样的记录工作,这真是太神奇了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.