使用特定的字符序列分割文件,并从源文件中提取文件名

使用特定的字符序列分割文件,并从源文件中提取文件名

我有一个大文件 (.sdf),我想使用特殊字符(查找 $$$$)将其拆分为更多小文件。

sdf文件是这样的:

vercirnon
 OpenBabel12162112363D

 30 32  0  0  0  0  0  0  0  0999 V2000
    9.5587    8.0626    0.2010 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.5370    6.8058   -0.7024 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.9692    7.2294   -2.0790 C   0  0  0  0  0  0  0  0  0  0  0  0
   10.9840    6.3066   -0.9295 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.7040    5.7067   -0.0175 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.1113    5.2073    1.2337 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.3733    4.2234    1.8957 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.2091    3.7212    1.3148 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.2780    2.4897    2.2251 S   0  0  0  0  0  0  0  0  0  0  0  0
    6.8993    1.1790    2.0362 O   0  0  0  0  0  0  0  0  0  0  0  0
    5.8588    2.9960    3.5333 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.9981    2.2950    1.5012 N   0  0  0  0  0  0  0  0  0  0  0  0
    3.8722    3.1731    1.3833 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.9973    4.5103    1.8031 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.9406    5.4112    1.6839 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.7284    4.9785    1.1608 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3923    6.0759    1.0574 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    1.5746    3.6564    0.7477 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6388    2.7433    0.8212 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.4135    1.3214    0.4021 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1426    0.4511    0.8208 O   0  0  0  0  0  0  0  0  0  0  0  0
    1.2975    0.9364   -0.5087 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.5786    1.7825   -1.3567 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4392    1.2632   -2.1615 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7443   -0.0390   -2.1595 N   0  0  0  0  0  0  0  0  0  0  0  0
   -1.6669   -0.4714   -2.8846 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0490   -0.8769   -1.3832 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9865   -0.4274   -0.5611 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.7891    4.1887    0.0683 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.5337    5.1733   -0.5887 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  1  0  0  0  0
  2  5  1  0  0  0  0
  5  6  2  0  0  0  0
  5 30  1  0  0  0  0
  6  7  1  0  0  0  0
  7  8  2  0  0  0  0
  8  9  1  0  0  0  0
  8 29  1  0  0  0  0
  9 10  2  0  0  0  0
  9 11  2  0  0  0  0
  9 12  1  0  0  0  0
 12 13  1  0  0  0  0
 13 14  2  0  0  0  0
 13 19  1  0  0  0  0
 14 15  1  0  0  0  0
 15 16  2  0  0  0  0
 16 17  1  0  0  0  0
 16 18  1  0  0  0  0
 18 19  2  0  0  0  0
 19 20  1  0  0  0  0
 20 21  2  0  0  0  0
 20 22  1  0  0  0  0
 22 23  1  0  0  0  0
 22 28  1  0  0  0  0
 23 24  1  0  0  0  0
 24 25  1  0  0  0  0
 25 26  1  0  0  0  0
 25 27  1  0  0  0  0
 27 28  1  0  0  0  0
 29 30  2  0  0  0  0
M  END
$$$$
BMCL2016_20
 OpenBabel12162112363D

 27 30  0  0  0  0  0  0  0  0999 V2000
    1.7921    2.8234   -0.2356 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1990    2.7653    0.4098 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.7558    1.3491    0.1528 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2150    0.2459    0.8374 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.7062   -1.0464    0.6362 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.7583   -1.2832   -0.2567 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.2901   -2.6928   -0.4837 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.1934   -3.6802   -0.7756 N   0  3  0  0  0  0  0  0  0  0  0  0
    3.6094   -3.8094   -1.9125 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.8079   -3.1344   -3.2109 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.6857   -2.3007   -3.2802 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.0661   -3.4129   -4.3038 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.6185   -4.7945   -1.8075 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6833   -5.2233   -0.4919 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.9431   -6.1495    0.1174 N   0  0  0  0  0  0  0  0  0  0  0  0
    2.2260   -6.3518    1.4133 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2272   -5.6463    2.0980 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5189   -5.9726    3.7741 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    3.9903   -4.6817    1.4401 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.6977   -4.4753    0.0988 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.3011   -0.1919   -0.9458 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.8099    1.1011   -0.7442 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1193    3.0497    1.9295 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.7571    4.5058    2.2596 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.6882    5.3885    1.6530 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.6783    5.2898    0.2371 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.0923    3.8800   -0.1877 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2 23  1  0  0  0  0
  2 27  1  0  0  0  0
  3  4  2  0  0  0  0
  3 22  1  0  0  0  0
  4  5  1  0  0  0  0
  5  6  2  0  0  0  0
  6  7  1  0  0  0  0
  6 21  1  0  0  0  0
  7  8  1  0  0  0  0
  8  9  2  0  0  0  0
  8 20  1  0  0  0  0
  9 10  1  0  0  0  0
  9 13  1  0  0  0  0
 10 11  2  0  0  0  0
 10 12  1  0  0  0  0
 13 14  2  0  0  0  0
 14 15  1  0  0  0  0
 14 20  1  0  0  0  0
 15 16  1  0  0  0  0
 16 17  2  0  0  0  0
 17 18  1  0  0  0  0
 17 19  1  0  0  0  0
 19 20  2  0  0  0  0
 21 22  2  0  0  0  0
 23 24  1  0  0  0  0
 24 25  1  0  0  0  0
 25 26  1  0  0  0  0
 26 27  1  0  0  0  0
M  CHG  1   8   1
M  END
$$$$
BMCL2016_19
 OpenBabel12162112363D

 24 26  0  0  0  0  0  0  0  0999 V2000
    7.4483    1.0368    0.6595 O   0  0  0  0  0  0  0  0  0  0  0  0
    8.0491    0.0940    1.1188 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.7614    0.3379    2.0658 O   0  0  0  0  0  0  0  0  0  0  0  0
    7.9192   -1.2687    0.5814 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.5726   -2.3934    1.0856 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.1380   -3.4812    0.3334 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.4995   -4.8363    0.4795 N   0  0  0  0  0  0  0  0  0  0  0  0
    7.8411   -5.6890   -0.4439 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.9259   -5.2387   -1.3981 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.1791   -6.3752   -2.4701 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    6.6087   -3.8841   -1.4962 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.2318   -3.0081   -0.6162 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.1404   -1.6587   -0.4670 N   0  3  0  0  0  0  0  0  0  0  0  0
    6.2065   -0.8240   -1.2132 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.8399   -0.6829   -0.5536 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.5003   -1.3309    0.6427 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2340   -1.1639    1.2097 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.2730   -0.3639    0.5911 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9259   -0.1249    1.2502 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9644   -0.2517    2.6331 F   0  0  0  0  0  0  0  0  0  0  0  0
    0.4125    1.1039    0.9805 F   0  0  0  0  0  0  0  0  0  0  0  0
    0.0346   -1.0091    0.8151 F   0  0  0  0  0  0  0  0  0  0  0  0
    2.6134    0.3005   -0.5873 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.8777    0.1379   -1.1594 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  2  0  0  0  0
  2  3  1  0  0  0  0
  2  4  1  0  0  0  0
  4  5  1  0  0  0  0
  4 13  2  0  0  0  0
  5  6  2  0  0  0  0
  6  7  1  0  0  0  0
  6 12  1  0  0  0  0
  7  8  1  0  0  0  0
  8  9  2  0  0  0  0
  9 10  1  0  0  0  0
  9 11  1  0  0  0  0
 11 12  2  0  0  0  0
 12 13  1  0  0  0  0
 13 14  1  0  0  0  0
 14 15  1  0  0  0  0
 15 16  2  0  0  0  0
 15 24  1  0  0  0  0
 16 17  1  0  0  0  0
 17 18  2  0  0  0  0
 18 19  1  0  0  0  0
 18 23  1  0  0  0  0
 19 20  1  0  0  0  0
 19 21  1  0  0  0  0
 19 22  1  0  0  0  0
 23 24  2  0  0  0  0
M  CHG  1  13   1
M  END

我想使用字符 $$$$ 分割此文件,然后删除它。此外,必须使用第一行(名称)和扩展名来重命名新文件.sdf。结果应该是这样的:

文件 1 =vercirnon.sdf

vercirnon
 OpenBabel12162112363D

 30 32  0  0  0  0  0  0  0  0999 V2000
    9.5587    8.0626    0.2010 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.5370    6.8058   -0.7024 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.9692    7.2294   -2.0790 C   0  0  0  0  0  0  0  0  0  0  0  0
   10.9840    6.3066   -0.9295 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.7040    5.7067   -0.0175 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.1113    5.2073    1.2337 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.3733    4.2234    1.8957 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.2091    3.7212    1.3148 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.2780    2.4897    2.2251 S   0  0  0  0  0  0  0  0  0  0  0  0
    6.8993    1.1790    2.0362 O   0  0  0  0  0  0  0  0  0  0  0  0
    5.8588    2.9960    3.5333 O   0  0  0  0  0  0  0  0  0  0  0  0
    4.9981    2.2950    1.5012 N   0  0  0  0  0  0  0  0  0  0  0  0
    3.8722    3.1731    1.3833 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.9973    4.5103    1.8031 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.9406    5.4112    1.6839 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.7284    4.9785    1.1608 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.3923    6.0759    1.0574 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    1.5746    3.6564    0.7477 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6388    2.7433    0.8212 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.4135    1.3214    0.4021 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1426    0.4511    0.8208 O   0  0  0  0  0  0  0  0  0  0  0  0
    1.2975    0.9364   -0.5087 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.5786    1.7825   -1.3567 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4392    1.2632   -2.1615 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.7443   -0.0390   -2.1595 N   0  0  0  0  0  0  0  0  0  0  0  0
   -1.6669   -0.4714   -2.8846 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0490   -0.8769   -1.3832 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9865   -0.4274   -0.5611 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.7891    4.1887    0.0683 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.5337    5.1733   -0.5887 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  1  0  0  0  0
  2  5  1  0  0  0  0
  5  6  2  0  0  0  0
  5 30  1  0  0  0  0
  6  7  1  0  0  0  0
  7  8  2  0  0  0  0
  8  9  1  0  0  0  0
  8 29  1  0  0  0  0
  9 10  2  0  0  0  0
  9 11  2  0  0  0  0
  9 12  1  0  0  0  0
 12 13  1  0  0  0  0
 13 14  2  0  0  0  0
 13 19  1  0  0  0  0
 14 15  1  0  0  0  0
 15 16  2  0  0  0  0
 16 17  1  0  0  0  0
 16 18  1  0  0  0  0
 18 19  2  0  0  0  0
 19 20  1  0  0  0  0
 20 21  2  0  0  0  0
 20 22  1  0  0  0  0
 22 23  1  0  0  0  0
 22 28  1  0  0  0  0
 23 24  1  0  0  0  0
 24 25  1  0  0  0  0
 25 26  1  0  0  0  0
 25 27  1  0  0  0  0
 27 28  1  0  0  0  0
 29 30  2  0  0  0  0
M  END

文件2=BMCL2016_20.sdf

 BMCL2016_20
 OpenBabel12162112363D

 27 30  0  0  0  0  0  0  0  0999 V2000
    1.7921    2.8234   -0.2356 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1990    2.7653    0.4098 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.7558    1.3491    0.1528 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2150    0.2459    0.8374 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.7062   -1.0464    0.6362 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.7583   -1.2832   -0.2567 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.2901   -2.6928   -0.4837 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.1934   -3.6802   -0.7756 N   0  3  0  0  0  0  0  0  0  0  0  0
    3.6094   -3.8094   -1.9125 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.8079   -3.1344   -3.2109 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.6857   -2.3007   -3.2802 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.0661   -3.4129   -4.3038 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.6185   -4.7945   -1.8075 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6833   -5.2233   -0.4919 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.9431   -6.1495    0.1174 N   0  0  0  0  0  0  0  0  0  0  0  0
    2.2260   -6.3518    1.4133 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2272   -5.6463    2.0980 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.5189   -5.9726    3.7741 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    3.9903   -4.6817    1.4401 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.6977   -4.4753    0.0988 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.3011   -0.1919   -0.9458 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.8099    1.1011   -0.7442 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.1193    3.0497    1.9295 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.7571    4.5058    2.2596 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.6882    5.3885    1.6530 O   0  0  0  0  0  0  0  0  0  0  0  0
    3.6783    5.2898    0.2371 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.0923    3.8800   -0.1877 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2 23  1  0  0  0  0
  2 27  1  0  0  0  0
  3  4  2  0  0  0  0
  3 22  1  0  0  0  0
  4  5  1  0  0  0  0
  5  6  2  0  0  0  0
  6  7  1  0  0  0  0
  6 21  1  0  0  0  0
  7  8  1  0  0  0  0
  8  9  2  0  0  0  0
  8 20  1  0  0  0  0
  9 10  1  0  0  0  0
  9 13  1  0  0  0  0
 10 11  2  0  0  0  0
 10 12  1  0  0  0  0
 13 14  2  0  0  0  0
 14 15  1  0  0  0  0
 14 20  1  0  0  0  0
 15 16  1  0  0  0  0
 16 17  2  0  0  0  0
 17 18  1  0  0  0  0
 17 19  1  0  0  0  0
 19 20  2  0  0  0  0
 21 22  2  0  0  0  0
 23 24  1  0  0  0  0
 24 25  1  0  0  0  0
 25 26  1  0  0  0  0
 26 27  1  0  0  0  0
M  CHG  1   8   1
M  END

文件 3 =BMCL2016_19.sdf

    BMCL2016_19
 OpenBabel12162112363D

 24 26  0  0  0  0  0  0  0  0999 V2000
    7.4483    1.0368    0.6595 O   0  0  0  0  0  0  0  0  0  0  0  0
    8.0491    0.0940    1.1188 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.7614    0.3379    2.0658 O   0  0  0  0  0  0  0  0  0  0  0  0
    7.9192   -1.2687    0.5814 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.5726   -2.3934    1.0856 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.1380   -3.4812    0.3334 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.4995   -4.8363    0.4795 N   0  0  0  0  0  0  0  0  0  0  0  0
    7.8411   -5.6890   -0.4439 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.9259   -5.2387   -1.3981 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.1791   -6.3752   -2.4701 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    6.6087   -3.8841   -1.4962 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.2318   -3.0081   -0.6162 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.1404   -1.6587   -0.4670 N   0  3  0  0  0  0  0  0  0  0  0  0
    6.2065   -0.8240   -1.2132 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.8399   -0.6829   -0.5536 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.5003   -1.3309    0.6427 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.2340   -1.1639    1.2097 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.2730   -0.3639    0.5911 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9259   -0.1249    1.2502 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.9644   -0.2517    2.6331 F   0  0  0  0  0  0  0  0  0  0  0  0
    0.4125    1.1039    0.9805 F   0  0  0  0  0  0  0  0  0  0  0  0
    0.0346   -1.0091    0.8151 F   0  0  0  0  0  0  0  0  0  0  0  0
    2.6134    0.3005   -0.5873 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.8777    0.1379   -1.1594 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  2  0  0  0  0
  2  3  1  0  0  0  0
  2  4  1  0  0  0  0
  4  5  1  0  0  0  0
  4 13  2  0  0  0  0
  5  6  2  0  0  0  0
  6  7  1  0  0  0  0
  6 12  1  0  0  0  0
  7  8  1  0  0  0  0
  8  9  2  0  0  0  0
  9 10  1  0  0  0  0
  9 11  1  0  0  0  0
 11 12  2  0  0  0  0
 12 13  1  0  0  0  0
 13 14  1  0  0  0  0
 14 15  1  0  0  0  0
 15 16  2  0  0  0  0
 15 24  1  0  0  0  0
 16 17  1  0  0  0  0
 17 18  2  0  0  0  0
 18 19  1  0  0  0  0
 18 23  1  0  0  0  0
 19 20  1  0  0  0  0
 19 21  1  0  0  0  0
 19 22  1  0  0  0  0
 23 24  2  0  0  0  0
M  CHG  1  13   1
M  END

有人可以建议一些代码来做到这一点吗?我寻找过类似的东西,但没有找到任何东西。

提前谢谢了。

答案1

csplit允许分割模式并创建(默认)名为xxii(文字xxii=递增数字)的文件,下一步只是循环文件并使用相应的第一行重命名它们并删除原始文件。

#/bin/bash
csplit --suppress-matched infile.sdf '/\$\$\$\$/' '{*}' &&
for file in xx* ; do
  mv "$file" "$(head -n1 "$file")".sdf
done &&
rm infile.sdf

答案2

尝试这样的事情:

$ perl -lne '
    if (/^\s*\$\$\$\$\s*$/) {
      $newfile = 1;
      next;
    };

    if ($. == 1 || $newfile) {
      open($FH, ">", "$_.sdf") || die "error opening $_.sdf for write: $!\n";
      $newfile = 0;
    }

    print $FH $_;
    close(ARGV) if eof' bigfile.sdf

该变量$newfile用作布尔标志来决定是否从输入中提取输出文件名,并将其打开以进行写入。 $newfile对于输入文件的第一行以及匹配行之后的每一行都为 true $$$$(忽略该行上可能存在的任何前导和/或尾随空格)。在其他时候它都是假的。

close(ARGV) if eof行会在每个输入文件末尾重置行计数器 ( $.),从而允许脚本处理多个输入文件。

注意:没有对输入进行健全性检查。该脚本是一个最基本的快速而肮脏的黑客,它只相信文件的第一行和后面的第一行$$$$是有效的文件名。如果在尝试打开文件进行写入时出现任何错误(例如,由于“权限被拒绝”或“设备上没有剩余空间”或其他错误),它至少会优雅地死亡并显示错误消息。

答案3

awk '
NR == 1         {filename = $0 ".sdf"}
/^\$\$\$\$$/    {getline; filename = $0 ".sdf"}
                {print > filename}
' file.sdf

在模式中,我们必须转义特殊字符\$

相关内容