0

I have file1 and file2 as shown below,

file1

(org8:0.00000001,org9:0.00000068,((org28:0.00137458,(org30:0.00146077,org4:0.00127170):0.00066024):0.00262422,(org14:0.00384582,(((org29:0.00000521,org7:0.00000413):0.00404562,((org11:0.00004385,org27:0.00003977):0.00296931,(org17:0.00323249,(org23:0.00302643,(reference:0.00024091,(org10:0.00007366,org22:0.00008615):0.00008038):0.00192129):0.00071853):0.00090006):0.00095495):0.00061577,(((org12:0.00505023,(org21:0.00538687,(org19:0.00236994,(org26:0.00178219,(org15:0.00000069,org20:0.00000207):0.00153483):0.00106831):0.00077035):0.00125496):0.00125293,(org18:0.00483293,(org25:0.00029401,org6:0.00028799):0.00425966):0.00069740):0.00031934,(org24:0.00458946,((org3:0.00008816,(org1:0.00000001,org2:0.00000001):0.00006484):0.00392960,(org5:0.00372961,(org13:0.00000103,org16:0.00000068):0.00160455):0.00269241):0.00063488):0.00035619):0.00040885):0.00115518):0.02417304):0.00340600);

file2

org1=xap119
org2=xap121
org3=xap859
org4=Xca
org5=Xcm
org6=Xcb
org7=Xcg
org8=Xcf
org9=Xpp
org10=Xcc
org11=Xcac
org12=Pc
org13=Xcat
org14=Xam
org15=Xamar
org16=Xcal
org17=Xac
org18=Xcavi
org19=Xcave
org20=Xcavce
org21=Xak
org22=Xcad
org23=Xacaj
org24=Xab
org25=Xcaa
org26=Xcava
org27=Xcmang
org28=Xcana
org29=XDAR33341
org30=Xae

I need to replace the words in file1, based on the matching words defined in file2.

The expected outcome is shown below,

(Xcf:0.00000001,Xpp:0.00000068,((Xcc:0.00137458,(Xae:0.00146077,Xca:0.00127170):0.00066024):0.00262422,(Xam:0.00384582,(((XDAR33341:0.00000521,Xcg:0.00000413):0.00404562,((Xcac:0.00004385,Xcmang:0.00003977):0.00296931,(Xac:0.00323249,(Xacaj:0.00302643,(reference:0.00024091,(Xcc:0.00007366,Xcad:0.00008615):0.00008038):0.00192129):0.00071853):0.00090006):0.00095495):0.00061577,(((Pc:0.00505023,(Xak:0.00538687,(Xcave:0.00236994,(Xcava:0.00178219,(Xamar:0.00000069,Xcavce:0.00000207):0.00153483):0.00106831):0.00077035):0.00125496):0.00125293,(Xcavi:0.00483293,(Xcaa:0.00029401,Xcb:0.00028799):0.00425966):0.00069740):0.00031934,(Xab:0.00458946,((xap859:0.00008816,(xap119:0.00000001,xap121:0.00000001):0.00006484):0.00392960,(Xcm:0.00372961,(Xcat:0.00000103,Xcal:0.00000068):0.00160455):0.00269241):0.00063488):0.00035619):0.00040885):0.00115518):0.02417304):0.00340600);

I tried with gedit replace, but not applicable here. Please help me to do the same.

Thank you in advance.

0

Try this command:

sed -e 's/org/export org/g' file2 > varset; source ./varset; rm varset; sed -r -e 's/(org[0-9]+)([^0-9])/${\1}\2/g' file1 | envsubst

This will output your desired text to standard output. If you want to permanently record these changes add > new_file_name to the end of the chain of commands.

In this chain of commands, sed first uses your file2 to set your variables as environment variables so that they can be accessed by the subsequent commands. It then adjusts the old names in file1 into the variable names that the shell will understand from our use of the source command. This is then piped to the envsubst command to substitute the environment variables contained in the output, so that ${org8} is interpreted as Xcf for example.

Note that this solution is somewhat unique to your particular circumstances and would need to be adjusted if you wanted to apply it more broadly.

*EDIT I've adapted the sed expression to work for either of the initial file1 that you've provided.

  • Thank you @Jeff H for your time and help. It works perfect for certain data sets. As you mentioned, this script is not working for few of the data sets. – user357294 Jun 15 at 11:54
  • Someone clever will probably be able to offer you a more generic solution to apply to your other cases. Do the other scenarios also have a "file2" file which follows the "old=new" format? – Jeff H. Jun 15 at 11:58
  • H ((org14/1-131541:0.11535,((org29/1-131541:0.00055,org7/1-131541:0.00055)1.000:0.10112,((org17/1-131541:0.07344,(org23/1-131541:0.07426,((org10/1-131541:0.00201,org22/1-131541:0.00243)1.000:0.02451,((org8/1-131541:0.00059,org9/1-131541:0.00215)1.000:0.07550,(org28/1-131541:0.02937,(org30/1-131541:0.03113,org4/1-131541:0.02933)1.000:0.01379)1.000:0.05763)1.000:0.46804)1.000:0.01933)1.000:0.01210)1.000:0.01788,(org11/1-131541:0.00149,org27/1-131541:0.00128)1.000:0.07772)1.000:0.01869)1.000:0.01109)1.000:0.00932,((org25/1-131541:0.00820,org6/1-131541:0.00807)1.000:0.11750,((org3/1-131541:0 – user357294 Jun 15 at 12:03
  • I've adjusted my original answer to work for either of your file1. Try it out. – Jeff H. Jun 15 at 12:16
  • It works for both type of data set. Thank you so much. – user357294 Jun 15 at 12:38

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.