Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Oct 2010
Posts: 14
Thanks: 5
Thanked 1 Time in 1 Post
Question Increment and find closest value until the end of the file is reached.

Hi all,

I have a file which looks like:


Code:
0   1.1985506
1   1.2237930
2   1.2159038
3   1.2668828
4   1.2650216
5   1.2474344
6   1.2817688
7   1.2721698
8   1.2665005
9   1.2826315
10  1.2797879
11  1.3201736
12  1.3116595
13  1.3361583
14  1.3309238
15  1.3521332
16  1.3593159
17  1.3558449
18  1.3792990
19  1.3990164
20  1.4039725
21  1.4026414
22  1.4141786
23  1.4655373
24  1.4627539
25  1.4684621
26  1.4994614
27  1.4573526
28  1.4736820
29  1.4868084
30  1.4942920

and i need to get the closest values that result from successive increments in second column (say for example 0.2) starting from the first value until the end of the file is reached, so my output would be in this case:


Code:
0   1.1985506
19  1.3990164

Note that the value resulting from the addition of the last matched value (1.3990164) and the increment (0.2), i.e. 1.5990164, is greater than the last value of the file (1.4942920), so that's why 1.3990164 is the last printed value.

Is this possible to do? I would appreciate an awk, perl or python suggestion, since those are the programing languages I use at work.
Sponsored Links
    #2  
Old 02-08-2012
Registered User
 

Join Date: Jun 2008
Location: Singapore
Posts: 186
Thanks: 3
Thanked 40 Times in 39 Posts

Code:
#! /bin/sh

awk '
BEGIN {
	delta=0.2

	# process first line
	getline
	value=$2
	print
}
{
	if ( $2-value > delta ) {
		print 
		value=$2
	}
}' input

The Following User Says Thank You to chihung For This Useful Post:
ezitoc (02-09-2012)
Sponsored Links
    #3  
Old 02-09-2012
Registered User
 

Join Date: Oct 2010
Posts: 14
Thanks: 5
Thanked 1 Time in 1 Post
Nice! Thank you!
Sponsored Links
Reply

Tags
awk, closest, increment, nearest, successive

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
stunnell log file has reached its limit beckyboo UNIX for Dummies Questions & Answers 13 09-12-2011 11:58 AM
Find a repeated number in a particular field and dont increment value of variable SBC Shell Programming and Scripting 2 08-16-2009 12:47 AM
sed match closest/nearest pattern sudheer1984 UNIX for Advanced & Expert Users 1 07-02-2009 03:59 AM
cpio: Ulimit reached for file output dustytina SCO 0 10-25-2007 08:19 PM
tar: 0511-194 Reached end-of-file before expected. Funky_ass Shell Programming and Scripting 4 08-07-2006 07:58 AM



All times are GMT -4. The time now is 04:44 AM.