Sponsored Content
Top Forums Shell Programming and Scripting Closest Number from a Range of Numbers Post 302547707 by yazu on Tuesday 16th of August 2011 11:37:34 AM
Old 08-16-2011
A typical task for binary search. But if you don't care about efficiency this works (GNU grep and bash):
Code:
nums='1, 3, 4, 6, 10, 30, 48, 49, 50, 51, 89999, 90000'

num=39

echo $nums, $num | tr , '\n' | sort -n | grep -C1 $num | xargs |             
  read a b c d; if ((b-a <= c-b));then echo $a; else echo $c; fi


Last edited by yazu; 08-16-2011 at 12:52 PM..
 

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
Chart::Clicker::Data::Range(3pm)			User Contributed Perl Documentation			  Chart::Clicker::Data::Range(3pm)

NAME
Chart::Clicker::Data::Range - A range of Data VERSION
version 2.83 SYNOPSIS
use Chart::Clicker::Data::Range; my $range = Chart::Clicker::Data::Range->new({ lower => 1, upper => 10 }); DESCRIPTION
Chart::Clicker::Data::Range implements a range of values. ATTRIBUTES
lower Set/Get the lower bound for this Range max Set/Get the maximum value allowed for this Range. This value should only be set if you want to EXPLICITLY set the upper value. min Set/Get the minimum value allowed for this Range. This value should only be set if you want to EXPLICITLY set the lower value. upper Set/Get the upper bound for this Range METHODS
add Adds the specified range to this one. The lower is reduced to that of the provided one if it is lower, and the upper is ADDED to this range's upper. combine Combine this range with the specified so that this range encompasses the values specified. For example, adding a range with an upper-lower of 1-10 with one of 5-20 will result in a combined range of 1-20. contains ($value) Returns true if supplied value falls within this range (inclusive). Otherwise returns false. divvy my $values = $range->divvy(5); Returns an arrayref of $N - 1 values equally spaced in the range so that it may be divided into $N pieces. span Returns the span of this range, or UPPER - LOWER. AUTHOR
Cory G Watson <gphat@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06-22 Chart::Clicker::Data::Range(3pm)
All times are GMT -4. The time now is 03:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy