Hello gurus,
I have two variable columns 1 and 2 , and their respective groups in 3 and 4
var1 var2 gr1 gr2
a b g h
c d h g
d f d h
f g h g
d r h d
p q a b
h y h g
r t g h
I want to rearrange the rows in such a way that all similarly grouped (var1 var2) pairs are together . The similarity rule is (gr1 gr2) pair is the same as (gr2 gr1) pair.
For example
the variable pair (a b) has group (g and h) , group (g and h) pair is equivalent to group (h and g) pair. Since variables (c d) has group pair of ( h and g) also equiavalent to (g and h), these can be clubbed together.
In other words columns $3""$4 is the same as $4""$3
So my desired output is
1 a b g h
1 c d g h
1 h y g h
1 r t g h
1 f g g h
2 d f d h
2 d r d h
3 p q a b
To achieve this I`m trying to put the last 2 columns in an array and output in a sorted way. Then I can sort by the last columns and get my result, , but it gives me a blank output.
awk '{delete a; s=x; a[$3];a[$4]; for (i=1;i<=length(a);i++) { s =s"__"a}; print $1,$2,s}' infile | sort -k3,3 | head
please assist, row order doesn't matter as long as similar groups are togther. Please note this is made up data, and groups have no alpha or numeric pattern.