Finding change in values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding change in values
# 1  
Old 05-19-2011
Finding change in values

I have an array

Code:
X = ( -100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100 )

I want to find the place where values change from negative to positive.
# 2  
Old 05-19-2011
Which programming language are you using?
# 3  
Old 05-19-2011
awk should be ok
# 4  
Old 05-19-2011
Code:
echo '-100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100' | nawk '{for(i=1;i++<NF;) if($i*$(i+1)<0) {print i+1;next}}'

# 5  
Old 05-19-2011
In case multiple changes should be handled:

Code:
awk 'BEGIN {
  n = split("-100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100", x)
  for (i = 0; ++i <= n;) {
    if (i > 1) 
      (x[i] > 0) == l || r = r ? r FS i : i
    l = x[i] > 0 
    }
  print "+/- changes in position(s):", r    
  }'

For example:

Code:
% awk 'BEGIN {
  n = split("100 -90 -80 -70 -60 -50 -40 30 40 -50 60 70 80 90 100", x)
  for (i = 0; ++i <= n;) {
     if (i > 1) 
      (x[i] > 0) == l || r = r ? r FS i : i
    l = x[i] > 0
   }
  print "+/- changes in position(s):", r
  }'
+/- changes in position(s): 2 8 10 11

# 6  
Old 05-19-2011
yep, good idea:
Code:
echo '-100 -90 -80 4 5 6 -70 -60 -50 -40 30 40 50 60 70 80 90 100' | nawk '{l="";for(i=1;i++<NF;) if($i*$(i+1)<0) {l=l FS i+1} print "+/- changes in position(s):" l}'

# 7  
Old 05-19-2011
Quote:
Originally Posted by vgersh99
yep, good idea:
Code:
echo '-100 -90 -80 4 5 6 -70 -60 -50 -40 30 40 50 60 70 80 90 100' | nawk '{l="";for(i=1;i++<NF;) if($i*$(i+1)<0) {l=l FS i+1} print "+/- changes in position(s):" l}'

Does this code work if the first element is 100 and not -100?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

2. Shell Programming and Scripting

Finding unique values in a hash (Perl)

Hi, I have a hash with unique keys associated with some data. my %FINALcontigs = ( 'mira_rep_c765:119reads**', 'ctctactggaagactgac', 'mira_rep_c7454:54reads**', 'atggatactgcgctgttgctaactactgga', 'mira_rep_c6803:12reads**', 'atcgactggatgcagggttgtggtttcta', ... (2 Replies)
Discussion started by: jdilts
2 Replies

3. UNIX for Advanced & Expert Users

Finding causes of slabinfo values

I'm doing a little work on assessing and improving server stability. As part of that, we're reviewing the contents of /proc/slabinfo. We see that sometimes a certain metric-- say, buffer_head-- spike prior to server instability. We're interested in learning what contributes to that as we trace... (0 Replies)
Discussion started by: treesloth
0 Replies

4. Shell Programming and Scripting

Finding index values

I have an array XOFFS of size N. I have VDISTMIN and VDISTMAX. I want to find the beginning and and index in XOFFS such that values are within Example Let XOFFS = (10 20 30 40 50) If VDISTMIN = -20 and VDISTMAX = 20 Then ISTR = 1 and IEND = 2 as XOFFS through XOFFS fall within the... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Programming

Finding range of values in an array

I have an array containing distances in ascending order, for example: distances = 100 120 150 170 200 280 300 .... I have a number, let's say v = 170 and a variation value, let's say var = 100 . I want to return the array indexes for which the distances cover the range (v - var) to (v +... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Finding absolute values greater than a certain value

Hi I am posting here for the first time. I am trying to write a script that reads a data file and tries to determine if any absolute values that are above 0.5 I was thinking it ought to be possible to do this with awk somehow. Are there any suggestions before I start reinventing the wheel? ... (4 Replies)
Discussion started by: jackygrahamez
4 Replies

7. Shell Programming and Scripting

finding values in between

Hi, I have been trying to find someone with this similar problem but I was out of luck. So I have a file that has two columns that look like this (for example): 10 20 40 50 45 60 90 130 So column 1 is start and column 2 is stop but what I want to do is find whats not represented... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Shell Programming and Scripting

finding field count escaping the blank values

Hi All I have a file.Below are few records of the file. sample.txt CPS,ES,843232910001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR735,ALL CALLS,01674840629 CPS,ES,843232670001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR734,ALL... (2 Replies)
Discussion started by: king007
2 Replies

9. UNIX for Dummies Questions & Answers

How to sort alphabetically after finding values

I have a list of people in a usage log and need to print the names and phone numbers of people with over 500 logins. I'd also like to display these names alphabetically. I have their total logins set to a variable named total. So far, I have very little in my awk script to do this: FS=":"... (4 Replies)
Discussion started by: doubleminus
4 Replies

10. UNIX for Dummies Questions & Answers

finding specific values in a within a file

Hi everyone, Can anyone guide me on how to search through a huge file and look on specific column and if it finds a discrepancy on that column that does not conform to the specified criteria, ie (1) Numeric and (3) alpha chars F123 or G333..etc, etc! then idientify it and redirect... (3 Replies)
Discussion started by: Gerry405
3 Replies
Login or Register to Ask a Question