find the highest number in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the highest number in the file
# 22  
Old 03-22-2006
with the same input you had specified

Code:
test.1.0.0.1.tar.gz
test1.1.0.0.1.tar.gz
test12.1.0.0.1.tar.gz
test2.1.2.1.2.tar.gz
test23.2.0.3.1.tar.gz
test3.1.0.0.1.tar.gz
test32.1.1.1.1.tar.gz
test33.1.1.1.1.tar.gz
test34.1.1.1.1.tar.gz
test4.2.2.2.2.tar.gz
test5.1.1.1.1.tar.gz
test56.1.1.1.1.tar.gz
test8.1.1.1.1.tar.gz

try the following code,
i had included some changes which will take care of stripping of test*
and *tar.gz
just execute the following script

Code:
# !/usr/bin/ksh

ini=10000
sum=0
max=0
cnt=1
finalver=""

cal()
{
  sum=$(($sum + $1 \* $ini))
  ini=$(($ini / 10))
}

cat datafile | while read ver1
do
ini=10000
sum=0
  ver=`echo $ver1 | sed 's/test//;s/^\.//;s/\.tar\.gz//'`
  echo $ver | sed 's/\./ /g' | while read v1 v2 v3 v4
  do
  if [ ! -z $v1 ]
  then
     cal $v1
     v1=""
  fi
if [ ! -z $v2 ]
  then
     cal $v2
     v2=""
  fi
  if [ ! -z $v3 ]
  then
     cal $v3
     v3=""
  fi
  if [ ! -z $v4 ]
  then
     cal $v4
     v4=""
  fi
  done
  if [ $sum -gt $max ]
  then
     max=$sum
     finalver=$ver1
  fi
done
echo "Final Version: $finalver"
exit 0

output: Final Version: test56.1.1.1.1.tar.gz

Is that what you had requested? Smilie
# 23  
Old 03-22-2006
I have modified vino's script.but have not performed much testing.pls check this out.I dont claim to be a better solution.but this should work..
What do you say vino.

here is the script
#! /bin/sh

maxversion=`(sed 's/\.//g' ver.txt | sort -n | tail -1)`

while read line
do
x=`echo "$line" | tr -d '.'`

if [ "$x" = "$maxversion" ]
then
echo "maxver is $line"
exit
fi
done < ver.txt
# 24  
Old 03-22-2006
Run the script against the systemali's latest input. It will not give the right answer. That script was written to cater to an earlier set of input.
# 25  
Old 03-22-2006
Hurray !!!!!!!!!! although its still raw..but it gets the job done...Smilie

(svpkg1-nd:admin1) $ cat testfile
-r-xr-xr-x 1 root other 715 Aug 23 2005 /spare3/ARCHIVE/test3.1.2.3.9.tar.gz
-r-xr-xr-x 1 root other 719 Aug 23 2005 /spare3/ARCHIVE/test3.1.2.3.4.tar.gz
-r-xr-xr-x 1 root other 1590 Nov 16 14:58 /spare3/ARCHIVE/test3.1.2.3.11.tar.gz
-r-xr-xr-x 1 root other 708 Aug 18 2005 /spare3/ARCHIVE/test3.1.2.3.10.tar.gz
-r-xr-xr-x 1 root other 1661 Oct 27 13:37 /spare3/ARCHIVE/test3.1.0.0.4.tar.gz
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $ flag=`cat testfile | awk '{print $9}' | cat -n | tee /tmp/ali |sed s/test3.// | sed s/.tar.gz// | sed 's/\.//g' | sort -n -r -k 2 | tr / ' '|sort -n -r -k 4| head -1 | nawk '{print $1}'`
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $
(svpkg1-nd:admin1) $ head -$flag /tmp/ali | tail -1 |nawk '{print $2}'

/spare3/ARCHIVE/test3.1.2.3.11.tar.gz ----> this is what i was looking for Smilie


Thank you every one for all your assistance....As always much appreciated Smilie
# 26  
Old 03-22-2006
Yes in that case it won't work.But as systemali mentioned before that he was able to yank filename "test*" and "tar.gz" from the list and kept numbers in ver.txt file.then we can run this script against that file.
This is another solution of the same prob.But anyway problem is already solved.
Smilie
# 27  
Old 03-22-2006
Output from sed

sed does not impact or change the input file, it uses memory to duplicate the file, called "pattern space", it works by copying the input file to memory and does its work using the memory version of the file. If you wish to perform further operations on the output, then I suggest you redirect the output to a file i.e. using the ">" character.

e.g. sed command > outputfile


Good luck!
Smilie
# 28  
Old 03-22-2006
sed output...

sed does not change or alter the original file whatsoever, it copies the file supplied to it from the command line to a region of memory called the "pattern space" it uses this to perform its operations and afterwards returns its output to the command line. If you want to perform an operation on the output from sed, then I suggest you redirect its output to a file using the ">" character. Then you can further manipulate the data without affecting the original file.

Good luck!
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

Only print the entries with the highest number?

Just want to say this is great resources for all thing Unix!! cat tmp.txt A 3 C 19 A 2 B 5 A 1 A 0 C 13 B 9 C 1 Desired output: A 3 B 9 C 19 The following work but I am wondering if there is a better way to do it: (4 Replies)
Discussion started by: chirish
4 Replies

3. Shell Programming and Scripting

Find highest number - working but need help!

Hello all, I am new to this and need some help or maybe steer me to the right direction! I wrote a script to get the highest number and prints it on the screen, the script basically asks the user to input numbers, and then prints the highest number! very simple it works like this $sh max.sh... (8 Replies)
Discussion started by: unknownsolo
8 Replies

4. Programming

Help with find highest and smallest number in a file with c

Input file: #data_1 AGDG #data_2 ADG #data_3 ASDDG DG #data_4 A Desired result: Highest 7 Slowest 1 code that I try but failed to archive my goal :( #include <stdio.h> (2 Replies)
Discussion started by: cpp_beginner
2 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. Shell Programming and Scripting

Bash, finding highest number in another file

i have a problem i am working on and am completely new to bash commands. I writing a script to read another file and output the max and Min number in the script. I must use variables to output the max and min numbers. grades = file with numbers in them. This is what i got so far. Thank You in... (3 Replies)
Discussion started by: ctanner10126
3 Replies

7. Shell Programming and Scripting

How to only display lines where a field has the highest number?

Hello Wondering if anybody can advise me how I can sort the below file so it only displays lines with the latest versions of an object? As you'll see some of the scripts listed in my file have more than one version number (version number is after the file extension). E.g. cdm_bri.pkb has... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

8. Shell Programming and Scripting

Extract the highest number out

Hi Gurus, I've using HPUX B.11.23 U ia64 with shell = sh. I've been having some problem get the highest number of this script. Actually I wanted to get the highest number from this listing (TEST123 data and based on this highest number, there will be email being sent out. For example,... (6 Replies)
Discussion started by: superHonda123
6 Replies

9. Shell Programming and Scripting

Displaying lines of a file which have the highest number?

Hello Wondering if anybody may be able to advise on how I can filter the contents of the following file: <object_name>-<version> <Instance> GM_GUI_code.fmb-4 1 GM_GUI_code.fmb-5 1 GM_GUI_code.fmx-4 ... (7 Replies)
Discussion started by: Glyn_Mo
7 Replies

10. Shell Programming and Scripting

Finding line with highest number in a file

Hi All, My file looks some thing like this, File 1: - A 10 B 30 C 5 D 25 E 72 F 23 now my requirement is to find the line with highest number in it, i;e the result should be E 72 Thanks in Advance (1 Reply)
Discussion started by: balu_puttaganti
1 Replies
Login or Register to Ask a Question