Rearrange rows by group pairs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rearrange rows by group pairs
# 1  
Old 06-19-2018
Rearrange rows by group pairs

Hello gurus,

I have two variable columns 1 and 2 , and their respective groups in 3 and 4

Code:
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


Code:
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.

Code:
awk  '{delete a;  s=x; a[$3];a[$4]; for (i=1;i<=length(a);i++)  {  s =s"__"a[i]};  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.
# 2  
Old 06-19-2018
Try
Code:
awk '
        {if (!(a[$3,$4]))       {if (a[$4,$3])  {X  = $4
                                                 $4 = $3
                                                 $3 = X
                                                }
                                 else            a[$3,$4]++
                                }
        }
1
' file

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 06-19-2018
I think you have made a mistake in your desired output based on your sample input.
here's another alternative - a bit verbose, but....
awk -f sen.awk myFile where sen.awk is:
Code:
{ g=(($3>$4)?($4 OFS $3):($3 OFS $4)) }
{ a[g]=(g in a)?a[g] ORS (SUBSEP $1 OFS $2): (SUBSEP $1 OFS $2) }
END {
   for (i in a) {
     gsub(SUBSEP,++j OFS,a[i])
     gsub(ORS,OFS i ORS,a[i])
     printf("%s%s\n", a[i], OFS i)
   }
}

results in
Code:
1 a b g h
1 c d g h
1 f g g h
1 h y g h
1 r t g h
2 p q a b
3 d f d h
3 d r d h


Last edited by vgersh99; 06-19-2018 at 07:40 PM..
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 06-19-2018
Another option using sort:

Code:
awk '
{
  if (!(a[$3,$4]) && a[$4,$3]) {
    X  = $4
    $4 = $3
    $3 = X
  }
  if (!(a[$3,$4])) a[$3,$4]=++grp;
  $1=a[$3,$4] FS $1
} 1 ' infile | sort -d

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 06-20-2018
ImageI'm afraid I missed the leading counter. Try
Code:
awk '
NR > 1  {if (!(a[$3,$4]))       {if (a[$4,$3])  {X  = $4
                                                 $4 = $3
                                                 $3 = X
                                                }
                                 else            a[$3,$4] = ++CNT
                                }
         print a[$3,$4], $0
        }

' file | sort -g
1 a b g h
1 c d g h
1 f g g h
1 h y g h
1 r t g h
2 d f d h
2 d r d h
3 p q a b

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To group the text (rows) by similar columns-names in a file

As part of some report generation, I've written a script to fetch the values from DB. But, unluckily, for certain Time ranges(1-9.99,10-19.99 etc), I don't have data in DB. In such cases, I would like to write zero (0) instead of empty. The desired output will be exported to csv file. ... (1 Reply)
Discussion started by: kumar_karpuram
1 Replies

2. Shell Programming and Scripting

Group by and translate rows to column

I've a comma separated file with data below: 61401370587,505,1;0402686146,123;2387936.0;20170812 61401370587,505,2;0401296221,34;3.0;20170811 61401370587,505,5;0431169322,123;2387936.0;20170812 My requirement is to group by using 1st,2nd column . And translate the 3rd column's row data... (1 Reply)
Discussion started by: bhagat-reena
1 Replies

3. Shell Programming and Scripting

Rearrange a file (2000 base64 strings in 1 row into 1 string by rows)

I have 1 row which contains abouts 20000 base64 string. e.g: /p4bdllBS8qcvW/69GUYej8nEv6gwt7UAYl0g==WZdjwTUQX9UEKsT/zWaZdQ==uI would like rearrange this file by base64 strings. So the output should be this ( 1 string in 1 row): 69GUYej8nEv6gwt7UAYl0g== WZdjwTUQX9UEKsT/zWaZdQ==How could I do... (4 Replies)
Discussion started by: freeroute
4 Replies

4. Shell Programming and Scripting

Using awk to rearrange fields

Hi, I am required to arrange columns of a file i.e make the 15th column into the 1st column. I am doing awk 'begin {fs=ofs=","} {print $15,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' ad.data>ad.csv the problem is that column 15 gets to column 1 but it is not comma separated with the... (10 Replies)
Discussion started by: seddoubt
10 Replies

5. Shell Programming and Scripting

Awk: group rows by id and simple conversion

Hi all, I am a newbie to awk and trying to learn by doing examples. I got stuck at this relatively simple conversion. The start file looks like: 1 2 "t1" 1 3 "h1" 2 1 "h1" 2 2 "h2" and I want to convert it into 1 t1:2, h1:3; 2 h1:1, h2:2; Thanks. (9 Replies)
Discussion started by: eagle_fly
9 Replies

6. Shell Programming and Scripting

Convert rows to columns group

Hi I have the input file following like this "AIX" "AIX 6.0" "AIX 7.0" "Redhat 8" "Redhat 9" "Redhat 5.0 Enterprise Linux" "Sun Solaris 9" "Sun Solaris 10", "Sun Microsystems" "Oracle" .................................Like this 2000 lines I need to convert this input into... (5 Replies)
Discussion started by: selvanraj
5 Replies

7. UNIX for Dummies Questions & Answers

Rearrange columns and rows with awk

Hello, I have the following problem I have two columns with numbers arranged as follows: x1 y1 x2 y2 .... .... x250 y250 Now I need them arranged as follows: "string a" x1 y1 x1 y2 "string b" "string a" x1 y2 x2 y2 (3 Replies)
Discussion started by: Tom46
3 Replies

8. Shell Programming and Scripting

rearrange a file

hi! in awk, i have a file like this: Trace1: WRIT,Trace2: BLAN,Trace3: BLAN, -47.2120018005371,,,39815.4809027778 -46.3009986877441,,,39815.4809027778 -46.277000427246,,,39815.4809143519 -46.7389984130859,,,39815.4809259259 -46.3460006713867,,,39815.4809259259... (10 Replies)
Discussion started by: riderman
10 Replies

9. Shell Programming and Scripting

Sort, group rows

I wrote script in bash which generates this report: User1,admin,rep,User2,shell,path1,x1,r1 User2,admin,rep,User7,shell,path1,x1,r1 User3,admin,rep,User4,shell,path1,x1,r1 User4,admin,rep,User3,shell,path1,x1,r1 User5,admin,rep,User1,shell,path1,x1,r1 User6,admin,rep,User5,shell,path1,x1,r1... (6 Replies)
Discussion started by: patrykxes
6 Replies

10. Shell Programming and Scripting

Need help with a script to rearrange columns

I have a file that is semi-colon delimited and the column headers are always the same but the column number is totally random each time this file is generated. I don't have the skills to make a script for this so maybe someone can help. I would like to be able to take this file which has over... (11 Replies)
Discussion started by: n3al10
11 Replies
Login or Register to Ask a Question