***convert from perl to bash***


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ***convert from perl to bash***
# 1  
Old 04-01-2009
***convert from perl to bash***

can any body translate the follwing script into one that works in bash?

#!/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__
# 2  
Old 04-01-2009
The obvious question being: "Why should we, if you have yet to show any attempts at solving this, and without knowing why you would even want to this?"
# 3  
Old 04-01-2009
If you dont want to then fine, but cut the attitude was only asking for help
# 4  
Old 04-01-2009
No duplicate or cross-posting, please read the rules.

Continue here:

https://www.unix.com/shell-programmin...scription.html

Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to convert to m-d-yyyy

I am using bash that when run downloads a file a verifies that there is data in it. What I am not able to do is have a user enter a date in any format they wish and have it converted to m-d-yyyy. Thank you :). Bash printf " Welcome to NGS analysis, checking for new files and creating a... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Convert bash to simple perl

please delete! (0 Replies)
Discussion started by: SkySmart
0 Replies

3. Ubuntu

Convert a bash to ash

hello everybody, i'm a beginner in ash and i want to convert this bash script to ash. this script send a xml file to a nagios server : #!/bin/bash PROGNAME=$(basename $0) RELEASE="Revision 0.3" print_release() { echo "$RELEASE" } print_usage() { echo "" echo "$PROGNAME... (6 Replies)
Discussion started by: mdijoux25
6 Replies

4. Programming

Convert Bash script to C

dear all, i need your advice for convert bash shell to C programming INDEX=/zpool1/NFS/INDEX/${1} SCRIPT=/zpool1/NFS/script/${1} LIST=SAMPLE cd ${SCRIPT} for i in `cat ${LIST}` do GETDATE=`echo ${i}|awk '{print substr($1,9,8)}'` /usr/xpg4/bin/awk -F ":" '{close(f);f=$4}{print >>... (2 Replies)
Discussion started by: zvtral
2 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ? #!/bin/perl -w my $VAR = "some value"; if ( $VAR =~... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Help! Need to convert bash shell to perl

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for 2 weeks now and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies

8. UNIX for Dummies Questions & Answers

Intern needing to convert bash to perl.

Hello All. I am very new to Linux and I am currently interning. I have been working on a project for a week and I have had no success. I have to convert bash shell into perl to decrypt and store files. Here is the code in Linux and Bash. Any help would be greatly appreciated. $... (0 Replies)
Discussion started by: freak
0 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

Convert bash to sh URGENT :(

Hi, I have to write a program to compare 2 files for copying. the program is ran on Solaris 5.8 like this: script.sh file1.txt file2.txt I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I... (2 Replies)
Discussion started by: Chris Jones
2 Replies
Login or Register to Ask a Question

Featured Tech Videos