awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi,

My input files is like this

axis1 0 1 10
axis2 0 1 5
axis1 1 2 -4
axis2 2 3 -3
axis1 3 4 5
axis2 3 4 -1
axis1 4 5 -6
axis2 4 5 1

Now, these are my following tasks

  1. Print a first column for every two rows that has the same value followed by a string.

  2. Match on the fourth column for every two rows and if

a. Both values are positive, print another column saying z=0,color=pink for both rows.
b. Both values are negative, print another column saying z=0,color=violet for both rows.
c. If first is positive and second is negative, print another column saying z=0,color=yellow for both rows.
d. If first is negative and second is positive, print another column saying z=0,color=brown for both rows.

So, My final output will be

link1 axis1 0 1 10 z=0,color=pink
link1 axis2 0 1 5 z=0,color=pink
link2 axis1 1 2 -4 z=0,color=violet
link2 axis2 2 3 -3 z=0,color=violet
link3 axis1 3 4 5 z=0,color=yellow
link3 axis2 3 4 -1 z=0,color=yellow
link4 axis1 4 5 -6 z=0,color=brown
link4 axis2 4 5 1 z=0,color=brown

I was able to print the first column as a series but didn't get to know how to make two values the same and at two row intervals.

What have you tried so far?

Hi Scrutinizer,

So far, I used excel to make the first column.

And I have tried this to match the fourth column

sort -k2n,2 -k3n,3 input.txt | awk '{print $1"\t"$2"\t"$3"\t"$4}' | awk '{p=$0; v=$4; getline; d=?($4, $4++)} if(d==d++)  {print "z=0,color=pink}'

But, no success. I am still working around it.

I'm not sure what you want to achieve with the sort operation - any link between your lines will be lost.
However, try this (which may still require some polishing):

awk     'BEGIN          {split ("v y b p", COL)}

         function prt   (arg1)
                        {print "link" cnt, arg1,  "z=0,colour=" COL[p+2*n+1]}

         NR%2           {cnt++
                         p = $NF>0;
                         OLD = $0
                         getline
                         n = $NF>0;
                         prt (OLD)
                         prt ($0)
                        }
        ' file
link1 axis1 0 1 10 z=0,colour=p
link1 axis2 0 1 5 z=0,colour=p
link2 axis1 1 2 -4 z=0,colour=v
link2 axis2 2 3 -3 z=0,colour=v
link3 axis1 3 4 5 z=0,colour=y
link3 axis2 3 4 -1 z=0,colour=y
link4 axis1 4 5 -6 z=0,colour=b
link4 axis2 4 5 1 z=0,colour=b
1 Like