0

I have a text file like this

sp|QBWMM1-2|PDCI_Mouse(741-770) 8864=mil (25/2.20/2.50)
sp|Pm345|Hisf_Mouse(613-640) 776=mil (25/2.20/2.50)
sp|P0065-2|Hila_Mouse(344-393)6543=mil (25/2.20/2.50)
sp|Q90081|Rira_Mouse(47-72) 7365=mil (25/2.20/2.50)
sp|QQQQQ1|Ubs_Mouse(162-190) 22=mil (25/2.20/2.50)

I have another text file like this

sp|QBWMM1-2|PDCI_Mouse complex subunit alpha OS=Mouse OX=90009 PE=1 SV=1
sp|Pm345|Hisf_Mouse Heat shock 70 kDa protein 1A OS=Mouse OX=90009 PE=1 SV=1
sp|O15012|SCCC_Mouse Protein transport protein Sec16A OS=MOUSE OX=90009 PE=1 SV=4
sp|P0065-2|Hila_Mouse Filamin-A OS=MOUSE OX=90009 PE=1 SV=4
sp|Q90081|Rira_Mouse Alpha-actin OS=Mouse OX=90009 PE=1 SV=2
sp|QQQQQ1|Ubs_Mouse Tubulin alpha8 chain OS=Mouse OX=90009 PE=1 SV=1
sp|QQQQQ2|Ubs_Mouse Plasta-3 OS=Mouse OX=90009 PE=1 SV=4

I want to merge them together keep the similar part and put the rest together. So for example, in text 1 and text 2, the following are similar:

sp|QBWMM1-2|PDCI_Mouse(741-770) 8864=mil (25/2.20/2.50)
sp|QBWMM1-2|PDCI_Mouse complex subunit alpha OS=Mouse OX=90009 PE=1 SV=1

the first part is the most important. they are considered similar because they have similar strings sp|QBWMM1-2|PDCI_Mouse

then I want to put them togther and it becomes

sp|QBWMM1-2|PDCI_Mouse complex subunit alpha OS=Mouse OX=90009 PE=1 SV=1 (741-770) 8864=mil (25/2.20/2.50)

So the output could be like this:

sp|QBWMM1-2|PDCI_Mouse complex subunit alpha OS=Mouse OX=90009 PE=1 SV=1 (741-770) 8864=mil (25/2.20/2.50)
sp|Pm345|Hisf_Mouse Heat shock 70 kDa protein 1A OS=Mouse OX=90009 PE=1 SV=1 (613-640) 776=mil (25/2.20/2.50))
sp|O15012|SCCC_Mouse Protein transport protein Sec16A OS=MOUSE OX=90009 PE=1 SV=4
sp|P0065-2|Hila_Mouse Filamin-A OS=MOUSE OX=90009 PE=1 SV=4 (344-393)6543=mil (25/2.20/2.50)
sp|Q90081|Rira_Mouse Alpha-actin OS=Mouse OX=90009 PE=1 SV=2 47-72) 7365=mil (25/2.20/2.50)
sp|QQQQQ1|Ubs_Mouse Tubulin alpha8 chain OS=Mouse OX=90009 PE=1 SV=1 (162-190) 22=mil (25/2.20/2.50)
sp|QQQQQ2|Ubs_Mouse Plasta-3 OS=Mouse OX=90009 PE=1 SV=4
  • 1
    What is sublime? Sublime Text? Then why the bash tag? I don't know "sublime", but probably you want to search for a tutorial on regular expressions. – Sparhawk Jan 4 at 2:46
  • @Sparhawk you are right, I updated my question to make it more comprehensive – Learner Jan 4 at 3:05
  • 2
    You have totally changed the question with that edit! But the answer is using join. Hopefully you can use that to search for a tutorial, but it's fairly straightforward. Good luck. – Sparhawk Jan 4 at 3:07
  • join -a 1 text1.txt text2.txt does not give me the answer but thanks for your point – Learner Jan 4 at 3:19
4

If file1 is your first text file, and file2 is your second text file, this should work in Bash:

join -a 1 <(sort -k1,1 file2) <(sed -E -e 's/([^\s])\(/\1 (/' file1 | sort -k1,1)

Join is called with two files as arguments, which two files (anonymous pipes or FIFOs actually) are the result of two process substitutions that take the form <(...)in the above command.

The second argument file contains a modified version of file1, having a space inserted before the first ( on each line so as to create a proper space-delimited join field; this modification is done by sed. The contents of both argument files are sorted based on the join field (i.e. field 1), as required by join. The -a 1 option ensures that non-pairable lines in file2 (i.e. the first argument file) are also output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.