Sponsored Content
Top Forums Shell Programming and Scripting Closest Number from a Range of Numbers Post 302547760 by Corona688 on Tuesday 16th of August 2011 01:52:28 PM
Old 08-16-2011
Slight modification of it for numbers after:

Code:
 awk -v VAL=39 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);
        if((VAL<=$1)&&(((!SET) || (D < DIFF))))
        {
                DIFF=D;
                X=$1;
                SET=1;
        }
}

END {   if(!SET)        print "No greater value found";
        else            printf("Closest was %s\n", X);  }' < data

---------- Post updated at 11:52 AM ---------- Previous update was at 11:46 AM ----------

This will do both:

Code:
awk -v VAL=39 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);

        if((VAL<=$1)&&(((!SETA) || (D < DIFFA))))
        {
                DIFFA=D;
                XA=$1;
                SETA=1;
        }

        if((VAL>=$1)&&(((!SETB) || (D < DIFFB))))
        {
                DIFFB=D;
                XB=$1;
                SETB=1;
        }
}

END {   if(!SETA)       print "No greater bound";
        else            printf("Closest greater bound was %s\n", XA);

        if(!SETB)       print "No lesser bound";
        else            printf("Closest lesser bound was %s\n", XB);   }' < data

It finds 48 and 30 respectively.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep numbers range

I want to grep a range of numbers in a log file. My log file looks like this: 20050807070609Z;blah blah That is a combination of yr,month,date,hours,minutes,seconds. I want to search in the log file events that happened between a particular time. like between 20050807070000 to 20050822070000... (1 Reply)
Discussion started by: azmathshaikh
1 Replies

2. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

3. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

4. Shell Programming and Scripting

delete rows between closest pattern or range

Hi I am having some problom deleting the lines between two specific lines in a file. need to delete lines between two closest lines. i.e need to find the closest range or pattern in a file with repeating patterns. Sample Input: WARNING <some text in n number of lines> ERROR:2597... (10 Replies)
Discussion started by: sudheer1984
10 Replies

5. UNIX for Dummies Questions & Answers

Frequency of a range of numbers

Hello, I have a column where there are values from 1 to 150. I want to get the frequency of values in the following ranges: 1-5 6-10 11-15 .... .... .... 146-150 How can I do this in a for loop? Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

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

7. Shell Programming and Scripting

grep for a range of numbers

Dear Friends, I want to know how to grep for the lines that has a number between given range(start and end). I have tried the following sed command. sed -n -e '/20030101011442/,/20030101035519/p' However this requires both start and end to be part of the content being grepped. However... (4 Replies)
Discussion started by: tamil.pamaran
4 Replies

8. UNIX for Dummies Questions & Answers

Grep for a range of numbers?

I am trying to extract specific information from a large *.sam file (it's originally 28Gb). I want to extract all lines that are on chr3 somewhere in the range of 112,937,439-113,437,438. Here is a sample line from my file so you can get a feel for what each line looks like: seq.4 0 ... (8 Replies)
Discussion started by: genGirl23
8 Replies

9. Shell Programming and Scripting

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies
UNIVERSAL(3perl)					 Perl Programmers Reference Guide					  UNIVERSAL(3perl)

NAME
UNIVERSAL - base class for ALL classes (blessed references) SYNOPSIS
$is_io = $fd->isa("IO::Handle"); $is_io = Class->isa("IO::Handle"); $does_log = $obj->DOES("Logger"); $does_log = Class->DOES("Logger"); $sub = $obj->can("print"); $sub = Class->can("print"); $sub = eval { $ref->can("fandango") }; $ver = $obj->VERSION; # but never do this! $is_io = UNIVERSAL::isa($fd, "IO::Handle"); $sub = UNIVERSAL::can($obj, "print"); DESCRIPTION
"UNIVERSAL" is the base class from which all blessed references inherit. See perlobj. "UNIVERSAL" provides the following methods: "$obj->isa( TYPE )" "CLASS->isa( TYPE )" "eval { VAL->isa( TYPE ) }" Where "TYPE" is a package name $obj is a blessed reference or a package name "CLASS" is a package name "VAL" is any of the above or an unblessed reference When used as an instance or class method ("$obj->isa( TYPE )"), "isa" returns true if $obj is blessed into package "TYPE" or inherits from package "TYPE". When used as a class method ("CLASS->isa( TYPE )", sometimes referred to as a static method), "isa" returns true if "CLASS" inherits from (or is itself) the name of the package "TYPE" or inherits from package "TYPE". If you're not sure what you have (the "VAL" case), wrap the method call in an "eval" block to catch the exception if "VAL" is undefined. If you want to be sure that you're calling "isa" as a method, not a class, check the invocand with "blessed" from Scalar::Util first: use Scalar::Util 'blessed'; if ( blessed( $obj ) && $obj->isa("Some::Class") { ... } "$obj->DOES( ROLE )" "CLASS->DOES( ROLE )" "DOES" checks if the object or class performs the role "ROLE". A role is a named group of specific behavior (often methods of particular names and signatures), similar to a class, but not necessarily a complete class by itself. For example, logging or serialization may be roles. "DOES" and "isa" are similar, in that if either is true, you know that the object or class on which you call the method can perform specific behavior. However, "DOES" is different from "isa" in that it does not care how the invocand performs the operations, merely that it does. ("isa" of course mandates an inheritance relationship. Other relationships include aggregation, delegation, and mocking.) By default, classes in Perl only perform the "UNIVERSAL" role, as well as the role of all classes in their inheritance. In other words, by default "DOES" responds identically to "isa". There is a relationship between roles and classes, as each class implies the existence of a role of the same name. There is also a relationship between inheritance and roles, in that a subclass that inherits from an ancestor class implicitly performs any roles its parent performs. Thus you can use "DOES" in place of "isa" safely, as it will return true in all places where "isa" will return true (provided that any overridden "DOES" and "isa" methods behave appropriately). "$obj->can( METHOD )" "CLASS->can( METHOD )" "eval { VAL->can( METHOD ) }" "can" checks if the object or class has a method called "METHOD". If it does, then it returns a reference to the sub. If it does not, then it returns undef. This includes methods inherited or imported by $obj, "CLASS", or "VAL". "can" cannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overridden "can" appropriately), so a return value of undef does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see perlsub) for methods they will handle via AUTOLOAD. For such 'dummy' subs, "can" will still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error. You may call "can" as a class (static) method or an object method. Again, the same rule about having a valid invocand applies -- use an "eval" block or "blessed" if you need to be extra paranoid. "VERSION ( [ REQUIRE ] )" "VERSION" will return the value of the variable $VERSION in the package the object is blessed into. If "REQUIRE" is given then it will do a comparison and die if the package version is not greater than or equal to "REQUIRE". Both $VERSION or "REQUIRE" must be "lax" version numbers (as defined by the version module) or "VERSION" will die with an error. "VERSION" can be called as either a class (static) method or an object method. WARNINGS
NOTE: "can" directly uses Perl's internal code for method lookup, and "isa" uses a very similar method and cache-ing strategy. This may cause strange effects if the Perl code dynamically changes @ISA in any package. You may add other methods to the UNIVERSAL class via Perl or XS code. You do not need to "use UNIVERSAL" to make these methods available to your program (and you should not do so). EXPORTS
None by default. You may request the import of three functions ("isa", "can", and "VERSION"), but this feature is deprecated and will be removed. Please don't do this in new code. For example, previous versions of this documentation suggested using "isa" as a function to determine the type of a reference: use UNIVERSAL 'isa'; $yes = isa $h, "HASH"; $yes = isa "Foo", "Bar"; The problem is that this code will never call an overridden "isa" method in any class. Instead, use "reftype" from Scalar::Util for the first case: use Scalar::Util 'reftype'; $yes = reftype( $h ) eq "HASH"; and the method form of "isa" for the second: $yes = Foo->isa("Bar"); perl v5.14.2 2011-09-26 UNIVERSAL(3perl)
All times are GMT -4. The time now is 05:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy