finding lowest numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding lowest numbers
# 1  
Old 12-12-2011
finding lowest numbers

i want to basically get the lowest numbers from a list ... for example my input file is ....


Code:
 
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 are 4,5,12
./getlowest 1 -> this gives me the next 1 lowest which is 4
./getlowest 5 -> this gives me the next 5 lowest which are 4,5,12,14,15

Thanks
# 2  
Old 12-12-2011
If you want a simple one-liner:
Code:
#!/bin/sh
# ${1-3} is the first parameter and, if blank, defaults to 3.
head -n "${1-3}" < data | awk -v OFS="," -v FS="\n" -v RS="" '{$1=$1} 1' OFS=','

More efficient would be to do everything in awk but that might not be as simple.

---------- Post updated at 10:47 AM ---------- Previous update was at 10:45 AM ----------

Actually:

Code:
awk -v MAX="${1-3}" '{ V=V","$1 }; NR==MAX { print substr(V,2); exit }' datafile

# 3  
Old 12-12-2011
Hi ...

its not working ...
for example ... awk -v MAX="${1-3}" '{ V=V","$1 }; NR==MAX { print substr(V,2); exit }' datafile gives me 1,2,3 from the above example ...but it should be 4,5,12

1,2,3 already exist in the file , i want the next available 3 lowest numbers ...

thx
# 4  
Old 12-12-2011
I misunderstood your question...

This may perhaps be because on looking at it again, I have no idea where your output is coming from at all. You say you want numbers from that file, but those numbers don't appear in your file at all, and don't seem to be derived from numbers in that file.

So I assumed by 'lowest numbers' you meant you wanted the lowest numbers from that file. The three lowest numbers would be the first three lines. The six lowest would be the first six lines. And so forth.
# 5  
Old 12-12-2011
i basically want the next lowest numbers which are "not in the input file" , they may or may not be in the sequence .

for example based on the above input ... they will be 4,5,12

i may not have explained it well ... sorry about that
# 6  
Old 12-12-2011
Based on the examples, I'm reading this as the next "n" missing numbers.

One way with Shell. Needs better validation of the parameter, but it does work.

Code:
max=$1
if [ ! ${max} -ge 1 ]
then
        echo "Invalid parameter"
        exit
fi
#
counter1=0
counter2=0
#
while true
do
        counter1=$(( $counter1 + 1 ))
        found=$(grep -xl ^${counter1} filename.txt) ; reply=$?
        if [ ! $reply -eq 0 ]
        then
                echo ${counter1}
                counter2=$(( $counter2 + 1 ))
                if [ ${counter2} -ge ${max} ]
                then
                        break
                fi
        fi
done

./getlower 5
4
5
12
14
15


Last edited by methyl; 12-12-2011 at 01:41 PM.. Reason: remove backticks for consistency
This User Gave Thanks to methyl For This Post:
# 7  
Old 12-12-2011
This seems to be working ...

nawk -v L=1 -v H=1000 '{a[$1]=$1;next} END{for(i=L;i<=H;i++) if(i in a) { ;} else { print i}}' |head -$numbers i need ....


Thanks for ur help Corona
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding distance between numbers

Hi, I have a file as ABC 1634230,1634284,1634349,1634468 1634272,1634301,1634356,1634534 What I want is to find distance between the numbers.. column 1 is the gene name and column 2 are starts and column 3 are their respective stops for the starts. So what I want is column 3 which has +1... (2 Replies)
Discussion started by: Diya123
2 Replies

2. Shell Programming and Scripting

finding common numbers (contents) across 2 or 3 files

I have 3 files which are tab delimited and have numbers in it. file 1 1 2 3 4 5 6 7 File 2 3 5 7 8 File 3 1 (4 Replies)
Discussion started by: Lucky Ali
4 Replies

3. Shell Programming and Scripting

Finding occurences of numbers

I have two files The first file is in following format 5 937 8 1860 5 1 683 2 1 129 2 2 5 938 8 1122 5 1 20 520 4 1860 1851 1 5 939 8 1122 1124 1 20 521 4 5883 14 6 1860 1852 1 683 4 2 (5 Replies)
Discussion started by: stuggler
5 Replies

4. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

5. UNIX for Dummies Questions & Answers

finding files with numbers in the file name???

Hello all, New to this forum! I got a Q: i want to find all files with numbers in the file name. e.g. blabla234.pm or fool654.pl action i took: ls | egrep '+' ls | egrep ls | egrep + ls | egrep '' ls | egrep '(+)' ls | egrep '()' ls | egrep '(.*.*)' ls | egrep '.*.*' ls | grep... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

6. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: denyal
8 Replies

7. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

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

9. Shell Programming and Scripting

Perl ? - How to find and print the lowest and highest numbers punched in by the user?

. . . . . . (3 Replies)
Discussion started by: some124one
3 Replies

10. IP Networking

finding port numbers

hither! whatz the command to find which process is using a specific port number? for example, port 8082? (3 Replies)
Discussion started by: darkcastle
3 Replies
Login or Register to Ask a Question