简体   繁体   中英

Merging two txt files using bash

I have: pull of txt files consisted of many strings and one reference.txt (short) file my bash script doing the following things: 1) while looping of pull of txt files it performs something with each of the files (not changing it composition) after which it 2) should merge composition of each files with the composition of reference.txt in specified position

eg the ending of each of workfiles looks like

ATOM  13703  HA  GLN   100      83.311  32.648  23.654  0.1232 1.3870
ATOM  13704  HG2 GLN   100      82.879  33.518  22.168  0.0203 1.4870
ATOM  13705  HB2 GLN   100      84.606  31.260  21.824  0.0452 1.4870
ATOM  13706 HE22 GLN   100      84.225  34.975  20.004  0.4304 0.6000
ATOM  13707  OE1 GLN   100      82.389  32.538  19.564 -0.6098 1.6612
ATOM  13708  NE2 GLN   100      83.817  34.212  19.506 -0.9574 1.8240
ATOM  13709 HE21 GLN   100      83.663  34.375  18.529  0.4304 0.6000
TER
END

here I should to substitute th last string "END" on the full text taken from reference.pdb

HETATM 5420 CU    CU A 559      54.926  67.840  66.647  1.00 47.11          CU  
HETATM 5421 MG    MG A 560      58.119  65.472  53.773  1.00 12.89          MG  
HETATM 5422 CA    CA A 561      78.459  57.926  52.401  1.00 22.70          CA 
HETATM 7924 CU    CU B 270      62.144  60.705  47.277  1.00 40.39          CU  
HETATM 7925 CU    CU B 271      60.809  60.526  45.118  1.00 44.84          CU

obtaining eventually for each of the lopped file smth like:

ATOM  13695  O   GLN   100      82.103  30.697  24.719 -0.8042 1.6612
ATOM  13696  CB  GLN   100      83.798  31.641  22.204 -0.0664 1.9080
ATOM  13697  CG  GLN   100      83.606  33.044  21.616 -0.0210 1.9080
ATOM  13698  CD  GLN   100      83.251  33.196  20.146  0.7093 1.9080
ATOM  13699  H   GLN   100      85.355  33.328  24.357  0.2681 0.6000
ATOM  13700  OXT GLN   100      83.889  29.761  24.981 -0.8042 1.6612
ATOM  13701  HG3 GLN   100      84.467  33.577  21.792  0.0203 1.4870
ATOM  13702  HB3 GLN   100      82.992  31.127  22.034  0.0452 1.4870
ATOM  13703  HA  GLN   100      83.311  32.648  23.654  0.1232 1.3870
ATOM  13704  HG2 GLN   100      82.879  33.518  22.168  0.0203 1.4870
ATOM  13705  HB2 GLN   100      84.606  31.260  21.824  0.0452 1.4870
ATOM  13706 HE22 GLN   100      84.225  34.975  20.004  0.4304 0.6000
ATOM  13707  OE1 GLN   100      82.389  32.538  19.564 -0.6098 1.6612
ATOM  13708  NE2 GLN   100      83.817  34.212  19.506 -0.9574 1.8240
ATOM  13709 HE21 GLN   100      83.663  34.375  18.529  0.4304 0.6000
TER
HETATM 5420 CU    CU A 559      54.926  67.840  66.647  1.00 47.11          CU  
HETATM 5421 MG    MG A 560      58.119  65.472  53.773  1.00 12.89          MG  
HETATM 5422 CA    CA A 561      78.459  57.926  52.401  1.00 22.70          CA 
HETATM 7924 CU    CU B 270      62.144  60.705  47.277  1.00 40.39          CU  
HETATM 7925 CU    CU B 271      60.809  60.526  45.118  1.00 44.84          CU
END

With sed, to insert the content of reference.pdb before END string :

sed '
/END/ {
r reference.pdb
a END
d
}
' workfile

Edit :

If the filename is stored in a variable, the sed command must be surrounded with double quotes :

ref=reference.pdb
sed "
/END/ {
r $ref
a END
d
}
" workfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM