Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coverting Perl Code to Unix Script

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
CA
Hello All,

I have a Perl Code as below. Because my input file is so large, hence Perl code modification will take for ever. However, I need to convert this Perl code to Unix Script. Please Help to write unix script that will perform the same job as the Perl code below. Thanx a bunch everyone for your time.

NOTE: The perl code below take one record from a file put it into array, modify certain element of the array and put the record back into a new file.


#!/usr/bin/perl
use strict ;
#use warnings ;
use Date::Manip ;

my (@input) ;
my ($input_record) ;
my ($output_file);
my ($input_file);
my ($array_record);
my (@array_record);
my (@array) ;
my ($name);
my ($lchar);
my ($date);
my (@array_file);
my (@names);
my ($new_record);

$input_file = "ytd_100.txt";
$output_file = "ytd_100.dat";

#check input file
(-e $input_file) || die ("input file does not exist \n") ;
open(INPUT, "$input_file") || die ("could not open infile\n");

open(OUTPUT, ">$output_file") || die ("could not open outfile \n") ;

@array_file = <INPUT> ;
foreach $array_record (@array_file){
@array = split (",", $array_record) ;

#Start Modifying Here

if (( $array[4] == 1) && ( $array[5] eq "A")) {
$array[4] = "BCGEU 2" ;
$array[5] = "REGULAR FT"
}

if (( $array[4] == 1) && ( $array[5] eq "C")) {
$array[4] = "BCGEU 2" ;
$array[5] = "REGULAR PT"
}

if (( $array[4] == 1 ) && ( $array[5] eq "F")) {
$array[4] = "BCGEU 2" ;
$array[5] = "SHORT TERM"
}

if (( $array[4] == 1 ) && ( $array[5] eq "E")) {
$array[4] = "BCGEU 2" ;
$array[5] = "SHORT TERM"
}
if (( $array[4] == 2) && ( $array[5] eq "A")) {
$array[4] = "BCGEU 1" ;
$array[5] = "REGULAR FT"
}

if (( $array[4] == 2) && ( $array[5] eq "C")) {
$array[4] = "BCGEU 1" ;
$array[5] = "REGULAR PT"
}

if (( $array[4] == 2) && ( $array[5] eq "F")) {
$array[4] = "BCGEU 1" ;
$array[5] = "SHORT TERM"
}

if (( $array[4] == 4) && ( $array[5] eq "A")) {
$array[4] = "EXCLUDED" ;
$array[5] = "REGULAR FT"
}

if (( $array[4] == 4) && ( $array[5] eq "C")) {
$array[4] = "EXCLUDED" ;
$array[5] = "REGULAR PT"
}

if (( $array[4] == 4) && ( $array[5] eq "F")) {
$array[4] = "EXCLUDED" ;
$array[5] = "SHORT TERM"
}

if (( $array[4] == 5) && ( $array[5] eq "A")) {
$array[4] = "EXCLUDED" ;
$array[5] = "SUPPORT FT"
}

if (( $array[4] == 5) && ( $array[5] eq "C")) {
$array[4] = "EXCLUDED" ;
$array[5] = "SUPPORT PT"
}

if (( $array[4] == 5) && ( $array[5] eq "F")) {
$array[4] = "EXCLUDED" ;
$array[5] = "SUPPORT ST"
}
$array[3] = " ";
chomp(@array) ;

#$array[12]=sprintf("%.2f", $array[12]);
$array[12]=$array[12]*0.010 ;

#$stop modifying here
$new_record = join ',', @array;
print OUTPUT "$new_record\n";

}
close (INPUT) ;
close (OUTPUT) ;
 
I think it could be quite a complicated search and replace.
You'd need one for each of my hash entries.
Possible in sed but why bother when perl can do it much more readably (as hopefully demonstrated in my code).


Trojan.
 
jayjaybigs

... where have you disappeared to?


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top