0

I've got a formatted file in this way:

1 223614 225119.537745 Engine45
2 223614 225121.082392 Engine45
3 223614 225124.440309 Engine45
4 223614 225124.763890 Engine45
5 223621 225124.933927 Engine46
6 223614 225124.934205 Engine45
7 223614 225125.354857 Engine45
8 223614 225127.603434 Engine45
.
.
.

I'm trying to make a awk/shell that takes that 1) verify columns 2 and 4 if there equal in the same line, and if this applies, 2) substract the second found value on column 2 with the first found column 1, should be like this:

1st line found by 1):

1 223614 225119.537745 Engine45

2nd line found by 1):

2 223614 225121.082392 Engine45

Output should be the answer of this:

225121.082392 - 225119.537745 = 1.544647

The next output should be :

3rd line find by 1):

3 223614 225124.440309 Engine45

4th line find by 1):

4 223614 225124.763890 Engine45

Output: 225124.763890 - 225124.440309 = 0.323581

And successively with all records in file provided.

I believe that 1) i could make it work but 2) is really getting hard for me but if someone can give a lead how to get could be very useful, i know basics from shell and awk if it helps, but i'm open to get done on another tools like perl .

Regards

cdjve
  • 3
  • 1
  • you want the output in to be in which place? – Avinash Raj May 18 '14 at 05:06
  • _1) verify columns 2 and 4 if there equal in the same line_ But there is only three columns available. – Avinash Raj May 18 '14 at 05:10
  • What should happen if the values are not the same? E.g. line 5 has `6` at the end. – choroba May 18 '14 at 05:11
  • 1
    You should provide your expected output to clarify. – anubhava May 18 '14 at 05:13
  • the output can be printed on screen, the columns are separated by space and in the file provided always will be and equal for each line, but this equal line could be right next or could be at final row, so script should search for the first match in file that has columns 2 and 4 equal and make the logic. – cdjve May 19 '14 at 22:23
  • @cdjve Is your issue resolved? Please update or leave a comment to explain what did not work for you. – jaypal singh May 23 '14 at 20:08

2 Answers2

0

You didn't specify what to do if the values are not equal. Do you want to skip the 2 lines? Or just one of them and search for a match?

Here is a Perl solution that skips both the lines:

perl -e '
    while (1) {
        @first =  split " ", <>;
        @second = split " ", <>;
        print $second[2] - $first[2], "\n" if $first[1] == $second[1] and $first[3] == $second[3];
        last if eof;
    }' input-file
choroba
  • 231,213
  • 25
  • 204
  • 289
0

If the values of two successive lines are not same and you want to skip the lines then you can do:

awk '!(NR%2) && $2==col2 && $4==col4{print $3-col3}{col2=$2;col3=$3;col4=$4}' file
1.54465
0.323581
2.24858

If you don't want to skip the lines and print the 3rd column as is then you can do:

awk '!(NR%2){print(($2==col2&&$4==col4)?$3-col3:col3 RS $3)}{col2=$2;col3=$3;col4=$4}' file
1.54465
0.323581
225124.933927
225124.934205
2.24858
jaypal singh
  • 74,723
  • 23
  • 102
  • 147