Get previous value in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get previous value in column
# 1  
Old 08-16-2016
Get previous value in column

Gents,

Please can you help.

Using the code below

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

I got
Code:
726 then 645

But my desired output is
Code:
644 then 726 then 645


Here is the file1
Code:
 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.Smilie
# 2  
Old 08-16-2016
Code:
awk '
        {
                A[++c] = $1
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        if ( i < c-1 )
                                printf "%d then %d then %d\n", A[i], A[i+1], A[i+2]
                }
        }
' file1

This User Gave Thanks to Yoda For This Post:
# 3  
Old 08-16-2016
Hi Yoda,

Thanks a lot.

You forgot the condition
Code:
if($1 < prev -50)

.

Please can you add this condition..
# 4  
Old 08-16-2016
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?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-16-2016
Hello jiam912,

Please provide more details about your problem along with what you have tried(both required each time). Could you please try following.
Code:
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.
Code:
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.
Code:
 
 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

Last edited by RavinderSingh13; 08-16-2016 at 12:26 PM.. Reason: Added non-one liner form of solutoin too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-16-2016
Code:
if ( ( i < c-1 ) && ( A[i+1] - A[i] > 50 ) )

This User Gave Thanks to Yoda For This Post:
# 7  
Old 08-16-2016
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fill column from previous row

Hi, I have the following content in file ABBR DESC COL3 COL4 COL5 COL6 AAA text desc aaa text text text text text text text text text text text text BBB text desc bbb text text text text text text text text CCC ... (10 Replies)
Discussion started by: bobbygsk
10 Replies

2. Shell Programming and Scripting

Fill data in column with previous value

Gents, Kindly help me. I have a file with empty values in selected column, I will like to fill the empty values with the previous value. Example Input file X 4959 30010 66727.00 20457.001 1 1441 66512.00 20234.00 20520.001 X 4959 30010 66727.00 20457.001 145 ... (7 Replies)
Discussion started by: jiam912
7 Replies

3. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

5. Shell Programming and Scripting

Read column values from previous and next line using awk

Hi, I have a csv file which contains data that looks something like this: Key1 Key2 Key3 New_Key1 New_Key2 New_Key3 102 30 0 - - - 102 40 1 30 40 50 102 50 2 40 50 ... (4 Replies)
Discussion started by: Nishi_Licious
4 Replies

6. Shell Programming and Scripting

Previous Column Value (addition)

Good day, First off, I would just like to say that I've been helped by a lot of the posts here in my own scripting work in the past few months. I've been a long time roamer, but first time poster. That being said, I'm stuck trying to figure out this issue (For computational research, not a... (4 Replies)
Discussion started by: Eblue562
4 Replies

7. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

8. Shell Programming and Scripting

eAdd two fields in a column if their previous field values are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output file as follows If field3 in file1 is same as field3 in the next line then the field4 should add... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies
Login or Register to Ask a Question