用于从文本文件中提取数据的Perl脚本

时间:2014-05-14 10:08:00

标签: perl

这是我music.txt档案中的数据

Seamus McGuire:The Wishing Tree:09-14-2000:14.95
Pat Kilbride:Loose Cannon:07-02-2000:15.95
Kevin Crawford:Seasons of Mists:06-23-2000:16.95
Prince:Purple Rain:01-01-1995:3.95
Meat Loaf:Bat out of Jell:03-03-1980:11.95
Eddie Money:Two Tickets:09-04-1979:8.98

我正在尝试编写一个

的Unix Perl脚本
  1. 询问用户的艺术家姓名(第一个或最后一个)

  2. 以格式化显示艺术家的全名,CD标题,日期和价格 输出。

  3. 预期

  4. 具有您选择的命令的正确标题

    有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

#!/usr/local/bin/perl
use strict;
use warnings;
print "Enter first or last name of artist: "; 
chomp(my $input = <STDIN>); #Take input from user
open (my $fh, "<", "music.txt") or die $!; #Open the file in read mode
while(chomp(my $line = <$fh>)){
        if($line =~ /$input/){ #Check if input matches with line 
                my @artist_info = split/:/,$line; #Split the data from line based on `:`
                print " Name: $artist_info[0]\n CD Title: $artist_info[1]\n Date: $artist_info[2]\n Price: $artist_info[3]\n";
        }
}
close($fh);

DEMO

相关问题