Shell脚本显示有问题,需要帮助

时间:2015-01-11 09:19:36

标签: linux shell grep

我在下面得到了这个代码,即当作者字段为空时搜索一本书。它将打印出匹配的数量,它将打印出匹配行的数量并显示该书的所有细节。但是,如果我现在运行此代码,它将显示在终端

中显示的信息

一些示例输入如下所示

The Hunger Games:Suzanne Collins:10:1:50
The Hunger Games:fake author:1:1:1

以及我使用的代码打印的内容如下所示

Found 2 Records
The Hunger Games
The Hunger Games    Suzanne Collins
Fake Author $10
1   50
1   25
1

我希望输出成为

 Found 2 records
 The Hunger Games, Suzanne Collins, $10, 1, 50
 The Hunger Games, Fake Author, $1, 1, 1

希望有人能够帮助我。谢谢

function search_book 
   {

       echo "Enter Title: "
       read title
       echo "Enter Author: "
       read author
       result_title=$(grep -ise "$title\:" BookDB.txt)
       record=$(grep -io "$title\:" BookDB.txt
       if [ "$result_title" != "" ] && [ "$result_title" == "$result_title" ] && [ "$author" == "" ]
       then
       title=$(echo "$result_title" | cut -f 1 -d ":")
       author=$(echo "$result_title" | cut -f 2 -d ":")
       price=$(echo "$result_title" | cut -f 3 -d ":")
       qty_ava=$(echo "$result_title" | cut -f 4 -d ":")
       qty_sold=$(echo "$result_title" | cut -f 5 -d ":")
       echo ""
       echo "Found" $record "records:"
       echo ""
       echo -e  "$title\t$author\t"\$"$price\t$qty_ava\t$qty_sold"
       fi

2 个答案:

答案 0 :(得分:1)

一个简单的Awk脚本将更加优雅和惯用,并且效率更高。

awk -F : -v title="$title" -v author="$author" '
    tolower($1)==tolower(title) && (author=="" || tolower($2)==tolower(author)) {
        a[++i]=$1 ", " $2 ", $" $3 ", " $4 ", " $5; next }
    END { if (i) print "Found " i " records:"
        for (j=1; j<=i; ++j) print a[j] }' BookDB.txt

如果您愿意在结尾而不是开头打印摘要,那么这可以更有效率和简化。

您可以编写一个简单的Bash包装器并将其保存在PATH的目录中,或将其包装在函数中。但是我建议不要让你的函数执行用户交互,因为这会使它们更难用作更复杂脚本中的构建块。

答案 1 :(得分:0)

以下脚本应满足您的要求:

#!/bin/bash

DB='./BookDB.txt'
TMP_FILE='/tmp/Book_Search.tmp.txt'

if [ ! -e "${TMP_FILE}" ]
then
        touch ${TMP_FILE}
fi

function search_book 
{
        echo "Enter Title: "
        read title
        echo "Enter Author: "
        read author
        if [ "${title}" = '' ] && [ "${author}" = '' ] # input test
        then
                echo 'You must provide Book Title and Author.'
        else
                grep -ise "${title}" ${DB} > ${TMP_FILE} #creates list of books matching title search criteria 
                index=0
                while read line
                do

                        title=$(echo ${line} | awk -F ":" {'print $1'})
                        author=$(echo ${line} | awk -F ":" {'print $2'})
                        price=$(echo ${line} | awk -F ":" {'print $3'})
                        qty_ava=$(echo ${line} | awk -F ":" {'print $4'})
                        qty_sold=$(echo ${line} | awk -F ":" {'print $5'})

                        query_result[${index}]=$(echo "${title}, ${author}, \$${price}, ${qty_ava}, ${qty_sold}") #creates new array element for every book matching search criteria
                        index=$(($index+1))
                done < ${TMP_FILE}
        fi

        lines=$(echo ${#query_result[@]}) #assigning lines variable the total count of array elements 
        echo "Found results: $lines"
        printf '%s\n' "${query_result[@]}" #using printf to print all array elements on new line

}

search_book

该脚本使用的是一个临时文件,其中写入了与您的搜索相关的所有发现。 当前输入测试是检查用户是否输入了两个输入的数据 - 书名和作者,但您必须更改grep条目才能更改将写入TMP_FILE的查询结果:

grep -ise "${title}" ${DB} > ${TMP_FILE} #after completion of this command there would be entries matching the ${title} search criteria in the ${TMP_FILE} temp file

您必须更改上面的条目才能匹配不同的搜索查询。 在$ {TMP_FILE}中收集数据后,我们正在使用while逐行读取数据并创建一个数组,其中包含我们要在成功完成脚本后打印的格式化数据。