Find min.max value if matching columns found using AWK

Input_ File :

2 3 4 5
1 1 0 1
2 1 -1 1
2 1 3 1
3 1 4 1

6 5 6 6
6 6 6 7
6 7 6 8

5 8 6 7

Desired output :

2 3 4 5
-1 1 4 1
6 5 6 8
5 8 6 7

Explanation:

If ( $2==$4) then
Minimum value of $1 and $3 =becomes==>col1
Maximum value of $1 and $3 =becomes==> col3

If($1==$3) then
Minimum value of $2 and $4 ==becomes==>col2
Maximum value of $2 and $4 ==becomes==>col4

Else
print as it is.

The following line should solve your problem.

awk '$2==$4 && $3!=$1{if($1>$3){min=$3;max=$1}else{min=$1;max=$3};$1=min;$3=max}$1==$3 && $2!=$4{if($2>$4){min=$4;max=$2}else{min=$2;max=$4};$2=min;$4=max}1' file

.. however I don't understand how you should get this output, can you elaborate :cool:

HI,

Thanks .

Its is simple that i need to check input file and i need to reduce the lines by two ways.

way1: checking column2 and column4 if it is equal, i will replace those line by single line with total 4 columns.

i.e ) column 1 contains min value
col3 contains max. value.
col 2 and col 4 is same

way2 : here as explained in the prev example.

It is actually reducing the records...

Hi, ur code is printing all the values... expected output is less lines as per the explained ways.

---------- Post updated at 11:53 AM ---------- Previous update was at 09:19 AM ----------

To be clear consider the below input_file

2 3 4 5
1 1 0 1
2 1 -1 1
2 1 3 1
3 1 4 1

here other than first record, all other records are $2 == $4, then i can reduce those $2==$4 records to a single record.

Desired output

2 3 4 5
-1 1 4 1                 (  =====> HERE -1 is min among $1&$3 and 4 is max among $1&$3)

The desired output contains second line with Col1 is min value(-1) of $2==$4 rows and Col2(4) is max value of $2==$4 rows.

awk '
END{if(t)print p[1],p[2],p[3],p[4]}
NF{
        t=$1==$3?2:(($2==$4)?1:0)
        if(l!=t){print p[1],p[2],p[3],p[4]}
        if(!t){printf;next}
        u=$t;if($t>$(t+2)){$t=$(t+2);$(t+2)=u}
        if(l==t){$t=$t<p[t]?$t:p[t];$(t+2)=$(t+2)>p[$(t+2)]?$(t+2):p[$(t+2)]}
        split($0,p);l=t
}' file