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
CPANPLUS::Configure(3pm)				 Perl Programmers Reference Guide				  CPANPLUS::Configure(3pm)

NAME
CPANPLUS::Configure - configuration for CPANPLUS SYNOPSIS
$conf = CPANPLUS::Configure->new( ); $bool = $conf->can_save; $bool = $conf->save( $where ); @opts = $conf->options( $type ); $make = $conf->get_program('make'); $verbose = $conf->set_conf( verbose => 1 ); DESCRIPTION
This module deals with all the configuration issues for CPANPLUS. Users can use objects created by this module to alter the behaviour of CPANPLUS. Please refer to the "CPANPLUS::Backend" documentation on how to obtain a "CPANPLUS::Configure" object. METHODS
$Configure = CPANPLUS::Configure->new( load_configs => BOOL ) This method returns a new object. Normal users will never need to invoke the "new" method, but instead retrieve the desired object via a method call on a "CPANPLUS::Backend" object. load_configs Controls whether or not additional user configurations are to be loaded or not. Defaults to "true". $bool = $Configure->init( [rescan => BOOL]) Initialize the configure with other config files than just the default 'CPANPLUS::Config'. Called from "new()" to load user/system configurations If the "rescan" option is provided, your disk will be examined again to see if there are new config files that could be read. Defaults to "false". Returns true on success, false on failure. can_save( [$config_location] ) Check if we can save the configuration to the specified file. If no file is provided, defaults to your personal config. Returns true if the file can be saved, false otherwise. $file = $conf->save( [$package_name] ) Saves the configuration to the package name you provided. If this package is not "CPANPLUS::Config::System", it will be saved in your ".cpanplus" directory, otherwise it will be attempted to be saved in the system wide directory. If no argument is provided, it will default to your personal config. Returns the full path to the file if the config was saved, false otherwise. options( type => TYPE ) Returns a list of all valid config options given a specific type (like for example "conf" of "program") or false if the type does not exist ACCESSORS
Accessors that start with a "_" are marked private -- regular users should never need to use these. See the "CPANPLUS::Config" documentation for what items can be set and retrieved. get_SOMETHING( ITEM, [ITEM, ITEM, ... ] ); The "get_*" style accessors merely retrieves one or more desired config options. set_SOMETHING( ITEM => VAL, [ITEM => VAL, ITEM => VAL, ... ] ); The "set_*" style accessors set the current value for one or more config options and will return true upon success, false on failure. add_SOMETHING( ITEM => VAL, [ITEM => VAL, ITEM => VAL, ... ] ); The "add_*" style accessor adds a new key to a config key. Currently, the following accessors exist: set|get_conf Simple configuration directives like verbosity and favourite shell. set|get_program Location of helper programs. _set|_get_build Locations of where to put what files for CPANPLUS. _set|_get_source Locations and names of source files locally. _set|_get_mirror Locations and names of source files remotely. _set|_get_fetch Special settings pertaining to the fetching of files. BUG REPORTS
Please report bugs or other issues to <bug-cpanplus@rt.cpan.org<gt>. AUTHOR
This module by Jos Boumans <kane@cpan.org>. COPYRIGHT
The CPAN++ interface (of which this module is a part of) is copyright (c) 2001 - 2007, Jos Boumans <kane@cpan.org>. All rights reserved. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. SEE ALSO
CPANPLUS::Backend, CPANPLUS::Configure::Setup, CPANPLUS::Config perl v5.16.2 2012-10-25 CPANPLUS::Configure(3pm)
All times are GMT -4. The time now is 03:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy