Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Reading a file and Classification Post 302294299 by Yogesh Sawant on Thursday 5th of March 2009 01:48:23 AM
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 More Discussions You Might Find Interesting

1. 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

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

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
KITSEND(1)						      General Commands Manual							KITSEND(1)

NAME
kitpost - posts distribution kits SYNOPSIS
kitpost [ -hrV ] [ -H file ] [ -D desc ] [ -m dest1,dest2 ] [ kits ] [ newsgroups ] DESCRIPTION
Kitpost posts distribution kits made by makedist to some (source) newsgroups specified on the command line. If you do not specify any kit list, then all the kits are sent. Otherwise, only the specified kits will be (re)sent. A kit list may include ranges, 1-10 specifying kits 1 through 10, and 5- meaning kits 5 up to the last one. You may also specify kit num- bers by separating them with commas or spaces, and even mix with ranges, such as: 1 3 5-7 9. Kitpost ensures correct References: lines are inserted in your postings so that all parts but the first point to the root article. Threaded newsreaders and end-users traditionally appreciate that. OPTIONS
The following options are recognized by kitpost: -h Print help message and exit. -m dest1,destn Sends kits by e-mail to the specified recipients. This option may be used in conjunction with newsgroup posting. It is mainly intended for sites where direct posting to a moderated newsgroup is not allowed by inews. You may thus send your kits to the newsgroup moderator in a form that will ease the whole posting process. -r Signals a repost. -D description Specify a description string that will be added to the subject line. Usually a brief sentence (less than, say, 40 characters). -H file Specify a file to be used as header introduction in the first part of your posting. Usually the root README file. -V Print version number and exit. BUGS
Article cross-referencing is properly set-up only when the whole package is (re)posted in one batch. It is not currently possible to set-up the article headers manually or better, interactively. AUTHOR
Raphael Manfredi <ram@hptnos02.grenoble.hp.com> SEE ALSO
makedist(1), kitsend(1). ram KITSEND(1)
All times are GMT -4. The time now is 11:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy