【问题标题】:Shell Script Display having trouble, need assistanceShell Script 显示有问题,需要帮助
【发布时间】:2015-01-12 16:51:55
【问题描述】:

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

一些示例输入如下所示

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

【问题讨论】:

  • 向我们展示这个脚本的输入。
  • 能否请您发布 BookDB.txt 的示例输入。我认为使用 awk 可以大大简化这段代码,以获取所有信息并打印出您想要的输出。
  • 嗨,我按要求添加了示例输入,抱歉我忘记了
  • 你真的应该展示你的确切脚本。您显示的内容将无法正常运行。至少有一个) 和一个} 缺失。检查结果标题是否等于自身也有点奇怪。

标签: linux shell grep


【解决方案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 的目录中,或者将其包装在一个函数中。但是,我建议不要让您的函数执行用户交互,因为这会使它们更难在更复杂的脚本中用作构建块。

【讨论】:

    【解决方案2】:

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

    #!/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 逐行读取它,并创建一个数组,其中包含我们要在脚本成功完成后打印的格式化数据。

    【讨论】:

      猜你喜欢
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 2011-05-22
      • 2011-01-14
      相关资源
      最近更新 更多