找到一个字符串并添加字符串 [英] find a string and add string

查看:154
本文介绍了找到一个字符串并添加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文件...看起来像:

I have multiple file...it looks like:

ATOM   1244  O   VAL   160       7.381  11.465  -2.646   1.000  60.790   0.000
ATOM   1245  CB  VAL   160       5.766  13.870  -0.914   1.000  57.640   0.000
ATOM   1246  CG1 VAL   160       7.232  14.074  -0.568   1.000  57.530   0.000
ATOM   1247  CG2 VAL   160       5.065  15.213  -1.089   1.000  58.420   0.000
ATOM   1248  N   VAL     1     -14.324 -25.299   8.568   1.000  77.840   0.000
ATOM   1249  CA  VAL     1     -14.315 -23.826   8.346   1.000  78.450   0.000
ATOM   1250  C   VAL     1     -15.181 -23.169   9.426   1.000  76.690   0.000

现在我必须在行后添加 TER:

Now i have to add "TER" after line:

ATOM   1247  CG2 VAL   160       5.065  15.213  -1.089   1.000  58.420   0.000

所有文件中的这一行都是相同的。因此,就像我应该在所有文件中找到此行,然后在此行之后添加 TER。

This line is same in all the files. So its like i should find this line in all the files and then add "TER" after this line..

我认为可以通过grep或sed搜索该行。但是如何在下一行添加字符串?我完全不清楚这段代码。.会有什么帮助吗?

I think line can be searched through grep or sed..but how can string be added in the next line? Iam not clear about this code at all..will some one help plz...

推荐答案

尝试在shell上运行它:

Try running this on shell:

/usr/bin/perl -p -i -e "s/(ATOM\s+1247\s+CG2\s+VAL\s+160\s+5.065\s+15.213\s+-1.089\s+1.000\s+58.420\s+0.000)/\$1\nTER/g" my.allfiles

-p使Perl在您的程序周围假设以下循环,这使得遍历文件名参数,就像sed:

"-p" causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed:


  1. LINE:

  2. while(<>) {

  3. ...#您的程序在这里

  4. }继续{

  5. 打印或死亡- p目标:$!\n;

  6. }

  1. LINE:
  2. while (<>) {
  3. ... # your program goes here
  4. } continue {
  5. print or die "-p destination: $!\n";
  6. }

如果文件名为由于某种原因无法打开参数,Perl会警告您,然后移至下一个文件。请注意,这些行是自动打印的。在打印过程中发生的错误被视为致命错误。要禁止打印,请使用-n开关。 -p会覆盖-n开关。

If a file named by an argument cannot be opened for some reason, Perl warns you about it, and moves on to the next file. Note that the lines are printed automatically. An error occurring during printing is treated as fatal. To suppress printing use the -n switch. A -p overrides a -n switch.

http ://perldoc.perl.org/perlrun.html

这篇关于找到一个字符串并添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆