Reading a file and Classification


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading a file and Classification
# 1  
Old 03-04-2009
Question Reading a file and Classification

Hello Everyone,

I am new to UNIX. I have got a requirement. Thought of posting it in this forum so that someone might help me. Please have a look at the scenario.

The Objective is to "classify books into four seperate files and then print a summary report".

Specifications are as follows:-

The books records are stored in one csv file and the layout and the contents are contained in File 1.
There are four categories of books: sports, computing, children's and horror. We should determine the category and then write a series of 3 records to the appropiate file.
The files need to be created in the program and their names will be: children, horror, sports and computing. The format of four files is contained in File 2.

Records which do not belong to any of the above categories should be printed to the screen as an error.

The summary report should be displayed on the screen and will contain the following information:
- Date
- names of files created (including path name)
- total number of boks processed
- total value of books

File 1 - CSV File sample
Code:
ISBN, Title, Category, cost, NO of copies
978-0340681138, Five Get into TRouble (Famous Five), Enid Blyton, Childrens, 6, 3
978-0142301883, The Little Match Girl, Hans Christian Anderson, Childrens, 3, 2
978-0304921532, cell, Stephen King, Horror, 7, 1
978-0596100292, Unix in a Nutshell Scripting, Arnold Robbins, Computing, 17, 2

File 2 - Format of output files
Code:
ISBN: 978-0340681138
Author: Enid Blyton
Title: Five Get into Trouble (Famous Five)

Could anyone give me some idea about how to achieve this requirement as I am a bit new to programming. I would appreciate your help.

Regards,
Yarlagadda.

Last edited by Yogesh Sawant; 04-01-2009 at 01:13 PM.. Reason: added code tags
# 2  
Old 03-05-2009
check if this perl script helps:
Code:
#!/usr/bin/perl
# classify_books.pl
my $csv_file   = shift;
my %categories = ( 'childrens' => 'childrens_books.txt',
                   'horror'    => 'horror_books.txt',
                   'sports   ' => 'sports_books.txt',
                   'computing' => 'computing_books.txt');

my @childrens_file, @horror_file, @sports_file, @computing_file;

open(CSV_FILE, '<', $csv_file)  or  die "Failed to read file $csv_file : $! \n";
while (my $line = <CSV_FILE>) {
    next unless ($line =~ m/^\d{3}-\d+,/);
    my ($isbn, $title, $author, $category) = split(/,/, $line);
    $title    =~ s/^\s+//;
    $author   =~ s/^\s+//;
    $category = lc($category);
    $category =~ s/^\s+//;

    unless (exists($categories{$category})) {
        print STDERR "\nUndefined category $category for book $title by $author having ISBN $isbn \n";
        next;
    }

    if ($category =~ m/childrens/) {
        push(@childrens_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/horror/) {
        push(@horror_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/sports/) {
        push(@sports_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
    elsif ($category =~ m/computing/) {
        push(@computing_file, "ISBN: $isbn\nAuthor: $author\nTitle: $title\n\n");
    }
}
close(CSV_FILE);

if (@childrens_file) {
    open (CHL_FILE, '>', $categories{'childrens'})  or  die "Failed to write file $categories{'childrens'} : $! \n";
    print CHL_FILE @childrens_file;
    close (CHL_FILE);
}

if (@horror_file) {
    open (CHL_FILE, '>', $categories{'horror'})  or  die "Failed to write file $categories{'horror'} : $! \n";
    print CHL_FILE @horror_file;
    close (CHL_FILE);
}

if (@sports_file) {
    open (CHL_FILE, '>', $categories{'sports'})  or  die "Failed to write file $categories{'sports'} : $! \n";
    print CHL_FILE @sports_file;
    close (CHL_FILE);
}

if (@computing_file) {
    open (CHL_FILE, '>', $categories{'computing'})  or  die "Failed to write file $categories{'computing'} : $! \n";
    print CHL_FILE @computing_file;
    close (CHL_FILE);
}

__END__


Last edited by Yogesh Sawant; 03-05-2009 at 03:52 AM..
# 3  
Old 03-05-2009
Question Need Shell Scripting!!

Hello Yogesh,

Thank you so much for your help. I appreciate your help. My requirement is to write the scripting in Shell Scripting. I am not into Pearl scripting. As I said in my first post I am very new to UNIX. So, I am working on Shell scripting at the moment. So, could you please help me in meeting the requirements in Shell scripting instead of Pearl Scripting.

Once again, thanks alot for your help.

Regards,
yarlagadda.
# 4  
Old 03-06-2009
if not perl, this could be done with help of awk
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

scripting for classification

hi i am very new to scripting. i am learning by myself. i found this example. can any one help in writing script for this example, so that i can have an idea how to analyse and script. example: overview: The aim of this exercise is to classify books into four seperate files and then print a... (1 Reply)
Discussion started by: yonex
1 Replies

2. UNIX Desktop Questions & Answers

CDE Classification Banner

Hello; I need to place a classification banner at the top of my Solaris 2.6 CDE Desktop. The Banner must be displayed in all CDE desktop sessions available on the CDE dashboard (1,2,3,4). The Banner must remain anchored at the top of the CDE desktop and must not be able to be closed or hidden... (0 Replies)
Discussion started by: rambo15
0 Replies

3. Shell Programming and Scripting

Creating a Classification Banner

Hello; I need to place a classification banner at the top of my Solaris 2.6 CDE Desktop. The Banner must be displayed in all CDE desktop sessions available on the CDE dashboard (1,2,3,4). The Banner must remain anchored at the top of the CDE desktop and must not be able to be closed... (1 Reply)
Discussion started by: rambo15
1 Replies
Login or Register to Ask a Question