1

Hi i am new to shell programming i hope u can guide me along thanks.

Hi i need a function to search either book title or author from a .txt file and echo out the following

Title:*Enter*
Author:Scissorhands

Found 3 records : C++ for dummies, John Scissorhands, $15.01, 10, 5 Java for dummies, Mary Scissorhands, $16.02, 20, 15 VB.NET for dummies, Edward Scissorhands, $17.03, 30, 25

eg

Found 3 records : 
C++ for dummies, John Scissorhands, $15.01, 10, 5 
Java for dummies, Mary Scissorhands, $16.02, 20, 15 
VB.NET for dummies, Edward Scissorhands, $17.03, 30, 25

my file format is as below

Book name:author:price:Qty:Qty Sold

harry potter:james:12.99:197:101

function Search_book
{
 FILE="/home/student/Downloads/BookDB.txt"
 echo found 1 records : $FILE contents
 cat FILE
}
4

3 回答 3

1

编辑:对不起,我误解了你的问题。

更新了混合 / 解决方案:

#!/bin/bash

read -p "Enter search term: " search
perl -ne '
    BEGIN{ $pattern = $ARGV[0]; shift; $n=0 }
    @a=split /:/;
    if ($a[0] =~ m/$pattern/i or $a[1] =~ m/$pattern/i) {
      print "$a[0], $a[1],\$$a[2],$a[3],$a[4]\n";
      $n += 1;
    }
    END{ print "Found $n title(s).\n" }
  ' "$search" /home/student/Downloads/BookDB.txt

输出:

$ ./search.sh
Enter search term: star wars vi
Title: Star wars VI - return of the jedi
Found 1 title(s).

并不是说您需要通配符的正则表达式(即.代替?.*代替*等),并且您不需要将通配符放在搜索词的开头/结尾以在给定(子)字符串中的任何位置查找匹配项。

当然,您也可以完全在 Perl 中执行此操作,而无需 shell 脚本包装器:

#!/usr/bin/env perl

use strict;
use warnings;

my $booklist = './books.txt';
my @book;

print "Enter search term: ";
chomp (my $pattern = <>);

open BOOKS, "<$booklist" or die $!;

my $n = 0;
foreach (<BOOKS>) {
  chomp;
  @book = split /:/;
  if ($book[0] =~ m/$pattern/i or $book[1] =~ m/$pattern/i) {
    print "$book[0], $book[1],\$$book[2],$book[3],$book[4]\n";
    $n += 1;
  }
}

close BOOKS;

print "Found $n title(s).\n";

有关解决方案,请参阅Adrian Frühwirth 的回答

于 2013-07-25T08:13:34.457 回答
1
search_book()
{
    awk -F':' -v search="$1" '$1 ~ search || $2 ~ search { i++; printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 } END { printf "%d records found\n", i }' books.txt
}

_

$ cat books.txt
X never marks the spot:Indiana Jones:9.99:1:1
A fistful of barnacles:Captain Twiddlymore:9.99:2:1
The time I blew up LeChuck:Guybrush Threepwood:8.99:100:60
When I blew up LeChuck:Guybrush Threepwood:8.99:100:50
Where I blew up LeChuck:Guybrush Threepwood:8.99:100:2

_

$ search_book Indiana
X never marks the spot, Indiana Jones,$9.99,1,1
1 records found

$ search_book Guybrush
The time I blew up LeChuck, Guybrush Threepwood,$8.99,100,60
When I blew up LeChuck, Guybrush Threepwood,$8.99,100,50
Where I blew up LeChuck, Guybrush Threepwood,$8.99,100,2
3 records found

$ search_book barnacle
A fistful of barnacles, Captain Twiddlymore,$9.99,2,1
1 records found

$ search_book foo
0 records found
于 2013-07-25T09:40:55.317 回答
0

@Wilson Turners 关于 Adrian 帖子的最后一点:

./book.sh 
Enter search term: barnacle
A fistful of barnacles, Captain Twiddlymore,$9.99,2,1
1 records found

以下是在现有代码中调用 awk 函数的方法:

#!/bin/bash
  search_book()
{
    awk -F':' -v search="$search" '$1 ~ search || $2 ~ search { i++; printf "%s, %s,$%s,%s,%s\n", $1, $2, $3, $4, $5 } END { printf "%d records found\n", i }' books.txt
}


read -p "Enter search term: " search

search_book
于 2013-07-27T11:27:01.793 回答