Sponsored Content
Top Forums Shell Programming and Scripting ***convert from perl to bash*** Post 302302808 by ferrycorsten73 on Wednesday 1st of April 2009 05:37:19 AM
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__
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. Shell Programming and Scripting

Convert bash to simple perl

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

10. 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
UDM_CAT_LIST(3) 							 1							   UDM_CAT_LIST(3)

udm_cat_list - Get all the categories on the same level with the current one

SYNOPSIS
array udm_cat_list (resource $agent, string $category) DESCRIPTION
Gets all the categories on the same level with the current one. The function can be useful for developing categories tree browser. PARAMETERS
o $agent - A link to Agent, received after call to udm_alloc_agent(3). o $category - RETURN VALUES
Returns an array listing all categories of the same level as the current $category in the categories tree. The returned array consists of pairs. Elements with even index numbers contain the category paths, odd elements contain the corresponding category names. $array[0] will contain '020300' $array[1] will contain 'Audi' $array[2] will contain '020301' $array[3] will contain 'BMW' $array[4] will contain '020302' $array[5] will contain 'Opel' ... etc. EXAMPLES
Following is an example of displaying links of the current level in format: Audi BMW Opel ... Example #1 udm_cat_list(3)example <?php $cat_list_arr = udm_cat_list($udm_agent, $cat); $cat_list = ''; for ($i=0; $i<count($cat_list_arr); $i+=2) { $path = $cat_list_arr[$i]; $name = $cat_list_arr[$i+1]; $cat_list .= "<a href="$_SERVER[PHP_SELF]?cat=$path">$name</a><br />"; } ?> SEE ALSO
udm_cat_path(3). PHP Documentation Group UDM_CAT_LIST(3)
All times are GMT -4. The time now is 08:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy