Get previous value in column

Gents,

Please can you help.

Using the code below

awk '{ if($1 < prev -50) {printf ("%4d then %4d \n",prev,$1) } ; prev = $1}' file1 

I got

726 then 645

But my desired output is

644 then 726 then 645

Here is the file1

 637 3007754557 10980 170800
 638 3051154569 10764 170823
 639 3007954557 10980 170834
 640 3051553681 15552 170859
 641 3050954569 10764 170902
 642 3008154557 10980 170910
 643 3051554281 15552 170916
 644 3050754569 10764 170940
 726 3051353141 10584 170948
 645 3008354557 10980 170950
 646 3051353681 15552 170954
 647 3051354281 15552 170958
 648 3050554569 10764 171018
 649 3051154281 15552 171058
 650 3008354569 10764 171119
 651 3050954281 15552 171140
 652 3007353933 15552 171155
 653 3008154569 10764 171201
 654 3050754281 15552 171222
 655 3007553933 15552 171232

Thanks for your help.:b:

awk '
        {
                A[++c] = $1
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        if ( i < c-1 )
                                printf "%d then %d then %d\n", A, A[i+1], A[i+2]
                }
        }
' file1
1 Like

Hi Yoda,

Thanks a lot.

You forgot the condition

if($1 < prev -50)

.

Please can you add this condition..

You have a format string for two integer values, you supply two integer variables, and your printout is two integers. Your desired output has three integer values. Where do you think you should start considering modifications?

1 Like

Hello jiam912,

Please provide more details about your problem along with what you have tried(both required each time). Could you please try following.

awk '{if((prev-$1>50 || $1-prev>50) && prev){Q=Q?Q " then "prev:prev}} {prev=$1} END{print Q " then " prev}'  Input_file

Output will be as follows.

644 then 726 then 655

Hello Yoda,

I think OP is asking to have difference of more than 50 in current and previous first field's value.

EDIT: Adding a non-one liner form of solution.

 
 awk '{if((prev-$1>50 || $1-prev>50) && prev){
                                                Q=Q?Q " then "prev:prev
                                            }
     }
     {
      prev=$1
     }
      END{
                print Q " then " prev
         }
    '   Input_file
 

Thanks,
R. Singh

1 Like
if ( ( i < c-1 ) && ( A[i+1] - A > 50 ) )
1 Like

Hi RudiC,

I want to compare the value in column 1, for each row if the value is lees than 50 compared ( prev - 50 ) with previous one... should be printed otherwise no...

The question was WHERE you would start to modify your insufficient attempt.

Howsoever, try

awk '{if($1 < prev -50) {printf ("%4d then %4d then %4d \n",prv2, prev, $1) } ; prv2 = prev; prev = $1}' file1
 644 then  726 then  645 
1 Like

Thanks a lot to all