Print each ; separated value, in front of the value preceding them

I have a file that looks like this,

P38112 Q12389; Q03532; P43586; Q08208; P37838; P40693; P38805
Q12389 P32892; P36049; Q03532; P43586; Q12176; P38112; P39744; P37838
P11154 P39940
P50094 P50095
P32892 Q12389; Q04660; P43586; P37838; P40693
Q05022 Q08235; Q12176; P39744
P40347 P32333
P63085 Q62108; Q9DBB1; P49841; Q03172; Q8CDB0; Q8R332-1; Q62132; Q9Z2B9
Q12749 Q07913
P32589 P02829; P08107; P10591; P11484
P53145 P38344; Q12522; Q02256
P38628 P40151
P53096 P14922; P61830
Q03497 P29366; P19073; P36006; Q04439; Q12163; P80667; P18851
Q39023 O80719; O64903
P32785 P00359

I want to print each ; separated value, in front of the value preceding them such that the preceding value also gets printed each time.
For example this is how I want the output to be,

P38112 Q12389 
P38112 Q03532 
P38112 P43586 
P38112 Q08208 
P38112 P37838 
P38112 P40693 
Q12389 P32892 
Q12389 P36049 
Q12389 Q03532 
Q12389 P43586

and so on...!

How can I do it using awk?
Any help would be highly appreciated.

Hello Syeda,

Could you please try following and let me know if this helps.

awk '{for(i=2;i<=NF;i++){sub(/\;/,X,$i);print $1 OFS $i}}' Input_file

Output will be as follows.

P38112 Q12389
P38112 Q03532
P38112 P43586
P38112 Q08208
P38112 P37838
P38112 P40693
P38112 P38805
Q12389 P32892
Q12389 P36049
Q12389 Q03532
Q12389 P43586
Q12389 Q12176
Q12389 P38112
Q12389 P39744
Q12389 P37838
P11154 P39940
P50094 P50095
P32892 Q12389
P32892 Q04660
P32892 P43586
P32892 P37838
P32892 P40693
Q05022 Q08235
Q05022 Q12176
Q05022 P39744
P40347 P32333
P63085 Q62108
P63085 Q9DBB1
P63085 P49841
P63085 Q03172
P63085 Q8CDB0
P63085 Q8R332-1
P63085 Q62132
P63085 Q9Z2B9
Q12749 Q07913
P32589 P02829
P32589 P08107
P32589 P10591
P32589 P11484
P53145 P38344
P53145 Q12522
P53145 Q02256
P38628 P40151
P53096 P14922
P53096 P61830
Q03497 P29366
Q03497 P19073
Q03497 P36006
Q03497 Q04439
Q03497 Q12163
Q03497 P80667
Q03497 P18851
Q39023 O80719
Q39023 O64903
P32785 P00359
 

I have removed only ; in the characters if you have more specific requirement please do mention the same, as I see apart from ; character like -1 also there in Input_file. Hope this helps.

Thanks,
R. Singh

1 Like

Yep, it works. Thanks a lot :slight_smile: