Avatar of dawber39
dawber39Flag for United States of America

asked on 

Shell Scripting

Wrote a script - that is handed a file name at the command line. The file is located in another profile and the profile location is in the script with the file name coming in as the argument. The script will read the 3rd field of each line in the file to determine which has the highest value. Then it will print that value +1 with the following text: "The new number is xxx" The script works - but I found it necessary to create a separate file "tmplist" with which to extract the third field to for comparison. So - my question is - "Is there a shorter way to write the following script  - using maybe an awk statement?"  
#!/usr/bin/perl
for $test(@ARGV) {
$counter++;
}
if ($counter != 1) {
print "You must enter a file name!\n";
}
if ($counter == 1) {
$flag = 0;
exec `cut -f3 -d":" /export/home/jones/public_html/$ ARGV[0] > "tmplist"`;
open (reader, "tmplist");
while ($line = <reader>) {
$test1 = $line;
if ($flag == 0) {
$test2 = $test1;
} # end if
$flag = 1;
if ($test2 < $test1) {
$test2 = $test1;
} #end if
} # end while
$new = $test2 + 1;
print "The new number is ";
print $new;
print "\n";
} # end if

Open in new window

PerlShell Scripting

Avatar of undefined
Last Comment
dawber39
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dawber39
dawber39
Flag of United States of America image

ASKER

I knew there has to be a shorter way

While testing this with the name test2 - I got this error:

Use of uninitialized value in numeric gt (>) at ./test2 line 18, <$fh> line 1.

I am not sure I understand that
Avatar of FishMonger
FishMonger
Flag of United States of America image

It's caused by $highest not being assigned an initial value.

Change line 15 to:
my $highest = 0;

Open in new window

Avatar of dawber39
dawber39
Flag of United States of America image

ASKER

Excellent - but I am now trying to make it print the new line - so it doesn't print the command line right after the output - it is being difficult. I appreciate your efforts
Avatar of dawber39
dawber39
Flag of United States of America image

ASKER

Got it - thank you for your help
Avatar of dawber39
dawber39
Flag of United States of America image

ASKER

What a time saver you guys are -I really appreciate  it.
Perl
Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects. Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

26K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo