Comparing lines of data | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Comparing lines of data

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-03-2012
markymarkg123 markymarkg123 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 January 2013, 3:21 PM EST
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
Apple Comparing lines of data

Total UNIX Rookie, but I'm learning. I have columns of integer data separated by spaces, and I'm using a Mac terminal.

What I want to do:
1. Compare "line 1 column 2" (x) to "line 2 column 2" (y); is y-x>=100?
2. If yes, display difference and y's line number
3. If no, increment x and y by one and repeat

How I am going about it:

Code:
sed -n '1,2 p' datafile | cut -d' ' -f1 #gives me the two fields of interest

Where do I go from here?
Sponsored Links
    #2  
Old 12-03-2012
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 July 2014, 12:19 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,664
Thanks: 182
Thanked 617 Times in 575 Posts
Is this a homework?
Sponsored Links
    #3  
Old 12-03-2012
markymarkg123 markymarkg123 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 January 2013, 3:21 PM EST
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
This is for research. The data is power and its odd harmonics, and I am looking for a way to ID transients without having to graph everything. I just chose simple numbers so I can understand the mechanics. Thanks.
    #4  
Old 12-03-2012
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 29 July 2014, 12:19 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,664
Thanks: 182
Thanked 617 Times in 575 Posts
OK,
please post a sample input file and an example of the expected output. Please use code tags: Video tutorial on how to use code tags in The UNIX and Linux Forums.
Sponsored Links
    #5  
Old 12-03-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts
With assumptions about your data and its structure:

Code:
awk '!(NR%2) && (d=$2-x)>=100{print d,NR}{x=$2}' file


Last edited by elixir_sinari; 12-03-2012 at 11:12 AM..
Sponsored Links
    #6  
Old 12-03-2012
markymarkg123 markymarkg123 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 January 2013, 3:21 PM EST
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
data sample, I modified the second column for simplicity:


Code:
1.94183e+05  7.8e+04  1.09609e+04  -2.51962e+03  2.58938e+03  -1.78130e+04  8.17326e+03  9.09750e+03  
1.95223e+05  7.8e+04  1.08129e+04  -2.50996e+03  2.23601e+03  -1.76457e+04 8.53983e+03  8.87947e+03  
1.95826e+05  7.8e+04  1.08992e+04  -2.80696e+03  2.35323e+03  -1.77662e+04  8.43230e+03  8.83312e+03  
1.94827e+05  7.62589e+04  1.11074e+04  -2.79142e+03  2.60123e+03  -1.80548e+04  8.05580e+03  8.95096e+03  
1.92552e+05  7.8e+04  1.10656e+04  -2.03271e+03  3.18014e+03  -1.78507e+04  7.77928e+03  9.32247e+03  
1.91165e+05  7.9e+04  1.07475e+04  -1.78148e+03  3.61320e+03  -1.76183e+04  7.69183e+03  9.39564e+03  
1.91355e+05  7.9e+04  1.07990e+04  -2.17165e+03  3.17912e+03  -1.79394e+04  7.87802e+03  9.13235e+03  
1.90294e+05  7.8e+04  1.08781e+04  -2.42501e+03  2.58355e+03  -1.82862e+04  8.15985e+03  8.91582e+03  
1.88968e+05  7.8e+04  1.03374e+04  -2.25564e+03  2.31198e+03  -1.81390e+04  8.48529e+03  8.75009e+03  
1.89367e+05  7.8e+04  1.01039e+04  -1.94181e+03  2.55877e+03  -1.76577e+04  8.55628e+03  8.80059e+03

---------- Post updated at 11:21 AM ---------- Previous update was at 11:19 AM ----------

and I expect that the output will be:
line 5: 1741.1
line 6: 1000

---------- Post updated at 11:27 AM ---------- Previous update was at 11:21 AM ----------

elixir_sinari, would you mind explaining?
Sponsored Links
    #7  
Old 12-03-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 6:27 AM EDT
Location: San Jose, CA, USA
Posts: 4,177
Thanks: 163
Thanked 1,423 Times in 1,207 Posts
Quote:
Originally Posted by markymarkg123 View Post
Total UNIX Rookie, but I'm learning. I have columns of integer data separated by spaces, and I'm using a Mac terminal.

What I want to do:
1. Compare "line 1 column 2" (x) to "line 2 column 2" (y); is y-x>=100?
2. If yes, display difference and y's line number
3. If no, increment x and y by one and repeat

How I am going about it:

Code:
sed -n '1,2 p' datafile | cut -d' ' -f1 #gives me the two fields of interest

Where do I go from here?
Your step 3 makes no sense to me. If y-x is not >= 100, then (y+1)-(x+1) can't be >= 100 either. This will just create an very large loop until one of the values overflows. So, until you can explain why this should be done, I'm going to ignore this requirement.

The proposal elixir_sinari makes the assumption that you are only comparing x values on odd numbered lines against y values on even numbered lines. Seeing your sample data and comments in message #6 in this thread, it looks like you want to compare the value in the 2nd column of your input file for every line except the 1st.

This slight modification to elixir_sinari's proposal:

Code:
awk '(NR>1) && (d=$2-x)>=100{print d,NR}{x=$2}' file

produces the output:

Code:
1741.1 5
1000 6
1741.1 14
1000 15
1741.1 23
1000 24

with your sample data. Is this what you wanted?
The Following User Says Thank You to Don Cragun For This Useful Post:
markymarkg123 (12-03-2012)
Sponsored Links
Closed Thread

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
Comparing the data in a 2 files i150371485 Shell Programming and Scripting 3 11-23-2012 08:09 AM
Extracting specific lines of data from a file and related lines of data based on a grep value range? Wynner Shell Programming and Scripting 3 05-18-2011 11:35 AM
Comparing lines of two different files mira Shell Programming and Scripting 3 05-17-2011 02:24 PM
comparing lines in file sigh2010 Shell Programming and Scripting 1 10-13-2009 05:35 PM
Comparing data list... giannicello UNIX for Dummies Questions & Answers 4 03-06-2003 12:08 PM



All times are GMT -4. The time now is 12:48 PM.