I have a text file with approx. 12 millions of lines, each composed of 4 fields (column 1, 2, 3 and 4)

Most lines have a unique STRING in column 2. I don't want to modify any of these lines.

Spread in the text file, I regularly have 2 consecutive lines with the same STRING in column 2. This roughly occurs 10,000 times in the entire text file. An example is shown hereunder:

```
column1 column2 column3 column4  
WT 1 ILS G  
WT 2 DSG E          
WT 3 WYT S 
. . . .  
WT 106  AAA X  
WT 106  BBB Y  
. . . .  
WT 2704 CCC X  
WT 2704 DDD Y   
. . . .  

```

What I would I like to achieve:

```
column1 column2 column3 column4  
WT 1 ILS G  
WT 2 DSG E          
WT 3 WYT S 
. . . .  
WT 106  BBB Y    
WT 106  AAA X  
. . . .   
WT 2704 DDD Y   
WT 2704 CCC X  
. . . .   

```

What resources do I have?

I have a text file with the 10,000 STRINGS that are present twice (106 and 2704 in my example here) and for which I need to swap the two lines.
I also know X and Y that are always the same in lines in which column2 is identical.

What I have done until now?

I know how to identify two lines with a common string (that I specify, for instance 106) and to swap them with sed.
What I don't know if how to make it dynamic (using the text file with the 10000 strings to identify) so I don't have I have to launch 10000 commands individually.

Thanks in advance for your time and help.
Best,

Laurent