Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing values from different lines 2

Status
Not open for further replies.

jkluesner

Technical User
Sep 29, 2006
24
US
Hello all,

I am a new (very new) user of awk and I am trying to write a if statement that will store a value in a variable, skip to the next line and store that same value (but different line) and then compare the two. If they are equal then loop again, if they are not equal then print the line. I will show you what the data looks like. I am looking at the hour/minute field. I want to be able to only print out the line when the first value on each minute changes. I want to skip the repeats of the minute. Thanks for the help.

05 09 2006 0627 26 59.9890-111 25.715 396
05 09 2006 0627 26 59.9790-111 25.696 397
05 09 2006 0627 26 59.9690-111 25.677 398
05 09 2006 0627 26 59.9580-111 25.657 399
05 09 2006 0628 26 59.9480-111 25.638 400
05 09 2006 0628 26 59.9370-111 25.619 401
05 09 2006 0628 26 59.9260-111 25.599 402
05 09 2006 0628 26 59.9150-111 25.580 403
05 09 2006 0628 26 59.9050-111 25.561 404
05 09 2006 0629 26 59.8950-111 25.541 405
05 09 2006 0629 26 59.8860-111 25.521 406
05 09 2006 0629 26 59.8770-111 25.500 407
05 09 2006 0629 26 59.8670-111 25.478 408
05 09 2006 0630 26 59.8580-111 25.458 409
05 09 2006 0630 26 59.8480-111 25.438 410
05 09 2006 0630 26 59.8370-111 25.418 411
05 09 2006 0630 26 59.8270-111 25.399 412
05 09 2006 0630 26 59.8150-111 25.380 413
05 09 2006 0631 26 59.8040-111 25.362 414


The hour/minute is the fourth field over looks like (0631)

This is what I have tried so far:

#!/usr/bin/awk -f

min = substr($0,14,1)
{next}
min2 = substr($0,14,1)
{
if (min != min2) print "$0"
else
next
}
 
So, where is the goal ?
Thread subject: comparing values from different lines
Last reply: I have only one line

???
 
I want to print the lines that are the first in each minute. I want to skip the repeats. In the first post you can see the repeat in minutes. I do not want any repeats.

Thanks!
 
Create a file named tmp3.txt with the following contents:
Code:
05 09 2006      0627  26 59.9890-111 25.715   396
05 09 2006      0627  26 59.9790-111 25.696   397
05 09 2006      0627  26 59.9690-111 25.677   398
05 09 2006      0627  26 59.9580-111 25.657   399
05 09 2006      0628  26 59.9480-111 25.638   400
05 09 2006      0628  26 59.9370-111 25.619   401
05 09 2006      0628  26 59.9260-111 25.599   402
05 09 2006      0628  26 59.9150-111 25.580   403
05 09 2006      0628  26 59.9050-111 25.561   404
05 09 2006      0629  26 59.8950-111 25.541   405
05 09 2006      0629  26 59.8860-111 25.521   406
05 09 2006      0629  26 59.8770-111 25.500   407
05 09 2006      0629  26 59.8670-111 25.478   408
05 09 2006      0630  26 59.8580-111 25.458   409
05 09 2006      0630  26 59.8480-111 25.438   410
05 09 2006      0630  26 59.8370-111 25.418   411
05 09 2006      0630  26 59.8270-111 25.399   412
05 09 2006      0630  26 59.8150-111 25.380   413
05 09 2006      0631  26 59.8040-111 25.362   414

What is the result of the following command typed at the shell prompt ?
Code:
awk '$4!=f4 {print;f4=$4}' tmp3.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
if I use a output file it looks like this:

05 09 2006 0627 26 59.9890-111 25.715 396
05 09 2006 0627 26 59.9790-111 25.696 397
05 09 2006 0627 26 59.9690-111 25.677 398
05 09 2006 0627 26 59.9580-111 25.657 399
05 09 2006 0628 26 59.9480-111 25.638 400
05 09 2006 0628 26 59.9370-111 25.619 401
05 09 2006 0628 26 59.9260-111 25.599 402
05 09 2006 0628 26 59.9150-111 25.580 403
05 09 2006 0628 26 59.9050-111 25.561 404
05 09 2006 0629 26 59.8950-111 25.541 405
05 09 2006 0629 26 59.8860-111 25.521 406
05 09 2006 0629 26 59.8770-111 25.500 407
05 09 2006 0629 26 59.8670-111 25.478 408
05 09 2006 0630 26 59.8580-111 25.458 409
05 09 2006 0630 26 59.8480-111 25.438 410
05 09 2006 0630 26 59.8370-111 25.418 411
05 09 2006 0630 26 59.8270-111 25.399 412
05 09 2006 0630 26 59.8150-111 25.380 413
05 09 2006 0631 26 59.8040-111 25.362 414

If I print to the prompt it prints the last line only.
 
Just to be sure, what is the result of the following command ?
sort -k4,4 -u tmp3.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It does the same thing in both print to prompt and print to file
 
Is by chance the EndOfLine a CarriageReturn instead of a LineFeed ?
awk 'BEGIN{RS="\r"}$4!=f4{print;f4=$4}' tmp3.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You are my hero!! It worked....that must have been it.

Thanks!!!!!!
 
PHV said:
I also wonder why thinking that a perl script converted from a non working awk program should do the trick ...

I dunno, just wondering whether it was some weird version of awk.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top