Increment and find closest value until the end of the file is reached.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment and find closest value until the end of the file is reached.
# 1  
Old 02-08-2012
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.
# 2  
Old 02-08-2012
Code:
#! /bin/sh

awk '
BEGIN {
	delta=0.2

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

This User Gave Thanks to chihung For This Post:
# 3  
Old 02-09-2012
Nice! Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

I need to find in a file a list of number where last two digit end in a range

I all I am tryng to find a way to sort a list of number in a file by the value of last two digit. i have a list like this 313202320388 333202171199 373202164587 393202143736 323202132208 353201918107 343201887399 363201810249 333201805043 353201791691 (7 Replies)
Discussion started by: rattoeur
7 Replies

2. Shell Programming and Scripting

Find and increment value in string of hex

I have a long string of hex (from ASN.1 data) where I need to find and change a particular hex value only and increment it. The hex pairs either side (84 and a7) of the value to increment will remain constant. i.e. "84 <length> <value_to_increment> a7" starting with 00. So end result: ... (11 Replies)
Discussion started by: securegooner
11 Replies

3. Shell Programming and Scripting

Bash Script Closest Timestamp (LOG FILE)

Hi All, I need something where I could take the date from FILE1 and in this case the date is Thu Sep 10 13:48:42 EDT 2015 and I need it to match the closest PREVIOUS date in the log file FILE2 this means in this specific case it would be Thu Sep 10 2015 13:35:28. I only need the closest... (3 Replies)
Discussion started by: roberto999
3 Replies

4. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

5. Shell Programming and Scripting

awk find closest

Hi All I am sorry but I have to open a new thread about my issue. My input files are: file_1 ID_1 10 ID_2 15 ID_3 32 ID_4 45 ID_5 66 ID_6 79 ID_7 88 file_2 ID_3 ID_5My output file should be ID_3 ID_1(-22) ID_2(-17) ID_4(-13) ID_5(34) (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

6. Shell Programming and Scripting

Find the closest value in another csv file preceding it and following it?

Hi, Is this possible? I want to take a csv file and find the closest value in another csv file preceding it and following it. For ex. In this csv file, I'll take the first line: 1309341156.800000000 1309341156.802500000 1309341156.805000000 1309341156.807500000 and find the closest... (2 Replies)
Discussion started by: superbbrr
2 Replies

7. UNIX for Dummies Questions & Answers

stunnell log file has reached its limit

Please help! I am really new to Linux, and my colleague who usually deals with these things isnt here to help me out. We are running Scalix mail services on CentOS 6.0. Email users with IMAP folders are getting an error message stating the server cannot be reached, however POP3 mail users are... (13 Replies)
Discussion started by: beckyboo
13 Replies

8. Shell Programming and Scripting

how to find the end tag in xml file.

Hi, I am newbie. I wanted to know how to find the end tag in shell where I have multiple tags in xml like <tag name="x" version="1.0"> </tag> <tag name="x" version="1.0"> </tag> <tag name="x" version="1.0"> </tag> And I wanted to update depends this xml as (in side the tag <subtag ... (10 Replies)
Discussion started by: mariakumar3
10 Replies

9. SCO

cpio: Ulimit reached for file output

I am trying run cpio backing up to tape, but I get the above error. Can anyone assist please as I am sleeping at work today. I tried setting the ulimit to unlimited but when I rebooted the server it came back with another value and I cannot run my backup now.... (0 Replies)
Discussion started by: dustytina
0 Replies

10. Shell Programming and Scripting

tar: 0511-194 Reached end-of-file before expected.

Hello everyone! I wrote a script for backing up a folder. It goes fine but today it started to spit out this error, when a folder is taring: tar: 0511-194 Reached end-of-file before expected. I didn't make any changes! - OS: UNIX AIX ibm 3 - the folder I'm trying to tar is 11Gb large... (4 Replies)
Discussion started by: Funky_ass
4 Replies
Login or Register to Ask a Question