Korn Shell - Finding lowest number of a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell - Finding lowest number of a file.
# 1  
Old 10-07-2009
Korn Shell - Finding lowest number of a file.

I'm writing a KSH script that will get a file on the command line (such as input.txt), and in this file there is on number per line. The program needs to take the file, read each and determine the lowest number in the file. I currently have a while loop setup that will correctly out put every number in the file, but I need a way to single each one out, determine if it's bigger than the previous and then will output the lowest.
# 2  
Old 10-07-2009
What did you try ? Post your script using [code] tags to see where you got stuck.
# 3  
Old 10-07-2009
I am attempting to do the same thing. I have a file named: Text1

the contents of this file:

5
10
30
50
1



My program looks like this:
Code:
read myVar1 < Text1
while read myVar2; do
       if [[ $myVar1<$myVar2 ]] then
                  print $myVar1
       else
                  print $myVar2
       fi
done < Text1


so this is what I am trying to do. The program will read the text file, it will then determine the lowest number in that file and print that number. I know that my program is not finished, but currently it just returns blank lines....wait no it now displays "Done unexpected". I have been fiddling with this for awhile and am really confused. BTW I am new to KSH

Last edited by pludi; 10-08-2009 at 02:17 AM..
# 4  
Old 10-08-2009
I am attempting to do the same thing. I have a file named: Text1

the contents of this file:

5
10
30
50
1



My program looks like this:
Code:
read myVar1 < Text1
while read myVar2; do
if [[ $myVar1<$myVar2 ]] then
print $myVar1
else
print $myVar2
fi
done < Text1


so this is what I am trying to do. The program will read the text file, it will then determine the lowest number in that file and print that number. I know that my program is not finished, but currently it just returns blank lines....wait no it now displays "Done unexpected". I have been fiddling with this for awhile and am really confused. BTW I am new to KSH

Last edited by pludi; 10-08-2009 at 02:15 AM.. Reason: Code tags, please...
# 5  
Old 10-08-2009
Code:
# awk '{max=(max>$1)?max:$1}END{print max}' file

Please read and understand the Forum Rules & Guidelines
# 6  
Old 10-08-2009
Korn

It looks like I am in the same situation but I am receiving a different output. Below is my script, ref, and output:

Script (.._.ksh):
Code:
read myVar1 < num.txt
while read myVar2; do
if [[ $myVar1<$myVar2 ]] then
print $myVar1
else
print $myVar2
fi
done < num.txt

Reference (num.txt):

1
15
25
80
10
0

Output:

1
1
1
1
1
0


Any help would be greatly appreciated.

-Anon

Last edited by pludi; 10-08-2009 at 02:21 AM.. Reason: code tags!!!
# 7  
Old 10-08-2009
Quote:
Originally Posted by MeatCookie
It looks like I am in the same situation but I am receiving a different output.
Quote:
Originally Posted by danmero
Please read and understand the Forum Rules & Guidelines
The forum rules & guidelines apply for each and every new member ! Please Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort from highest to lowest number

Hi Guys, I am looking for a way to sort the output below from the "Inuse" count from Highest to Lowest. Is it possible? Thanks in advance. user1 0.12 0.06 0 0.12 User Inuse Pin Pgsp Virtual Unit:... (4 Replies)
Discussion started by: jaapar
4 Replies

2. Shell Programming and Scripting

Korn Shell manipulating the string into dynamic currency number

Conversion of string into currency value.. ex1: number_of_positions=2 input_string=345987 Output= 345,987.00 ex2: number_of_positions=4 input_string=1345987 Output= 1,345,987.0000 Please respond as soon as possible edit by bakunin: we will gladly respond as soon as... (15 Replies)
Discussion started by: suren.bills
15 Replies

3. Shell Programming and Scripting

Perl: find next available lowest number that is available in two arrays

Hi there. I have a number allocation problem whereby I have 2 arrays built from 2 different sources. The arrays will just contain a listed of sorted numbers @a 1 7 10 14 15 16 @b 1 7 10 11 14 15 16 (2 Replies)
Discussion started by: hcclnoodles
2 Replies

4. Shell Programming and Scripting

finding lowest numbers

i want to basically get the lowest numbers from a list ... for example my input file is .... 1 2 3 6 7 8 9 10 11 13 Now i want to create a script or a one liner which i can use like this ... for example ..../getlowest 3 --> this gives me the next 3 lowest numbers which... (6 Replies)
Discussion started by: greycells
6 Replies

5. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

6. Shell Programming and Scripting

print every 20 lines the lowest number

Hello all, How can I find the lowest number every 10 lines? For example i have a list name1 -0.1 name2 2 name3 3 name4 -3 name5 1 name6 2 name7 34 name8 34 (6 Replies)
Discussion started by: TheTransporter
6 Replies

7. UNIX for Dummies Questions & Answers

Finding the lowest sequenced file in a directory?

Hello, I have three files in a directory: 1_700_123456.lst 1_701_123456.lst 1_702_123456.lst I am trying to use a command via ksh that will list the file that has the lowest number in the second node and put that to a file. In the example above, it would put 1_700_123456.lst... (2 Replies)
Discussion started by: stky13
2 Replies

8. Shell Programming and Scripting

Max number of parameters to korn shell?

Hi All, what is the maximum limit for the command line arguments in korn shell. Regards, Raju (4 Replies)
Discussion started by: rajus19
4 Replies

9. Shell Programming and Scripting

generate random number in korn shell

I want to be able to generate a random number within a korn shell script.. Preferably i would like to be able to state how many digits should be in this random number... ie 4 digits or 5 digits etc Any ideas? (2 Replies)
Discussion started by: frustrated1
2 Replies

10. Shell Programming and Scripting

Finding Occurence of comma in a Variable ( KORN Shell)

All I want to find the occurence of comma in a variable in KORN shell. For example : var = test,test2,test3 .... finding occurence of comma in this variable. Result = 3 now. Please help me to write the code. Thanks in advance. Regards Deepak (2 Replies)
Discussion started by: DeepakXavier
2 Replies
Login or Register to Ask a Question