查找,替换,在绳子的occurence增加 [英] Find, replace, and increment at each occurence of string

查看:172
本文介绍了查找,替换,在绳子的occurence增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的脚本并为此痛苦简单的问题提前道歉。我相信我已经搜查pretty彻底,但显然没有任何其他答案或食谱已经足够明确,我听不懂(如<一个href=\"http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file\">here - 还是没能得到它)。

我有一个由字母串(DNA,如果你在乎)的文件,每行一个字符串。上述每个字符串我插入另一条线,以确定潜在的字符串。对于那些你谁是生物信息学家,我试图弥补测试数据FASTA格式设置,也许你有工具吗?不管怎样,我把一个独特的一句话,民,每个>后,用一个bash增量和sed创建一个唯一的编号标题每个字符串的意向。例如,在data.txt中,我有...


  

> NUM,胡说,胡说,胡说


  
  

ATCGACTGAATCGA


  
  

> NUM,胡说,胡说,胡说


  
  

ATCGATCGATCGATCG


  
  

> NUM,胡说,胡说,胡说


  
  

ATCGATCGATCGATCG


我想它是...


  

> 0,胡说,胡说,胡说


  
  

ATCGACTGAATCGA


  
  

> 1,胡说,胡说,胡说


  
  

ATCGATCGATCGATCG


  
  

> 2,胡说,胡说,胡说


  
  

ATCGATCGATCGATCG


该解决方案可以是任何语言,只要它是完整的和放大器;&放大器;能够完成任务。我有SED,AWK,庆典和C ++(小==比没有经验稍多)一点经验。我知道,我知道,我需要学习的Perl,但我只是刚刚开始。现在的问题是:如何更换数与一个数字,每更换递增?如果底层串是相同的另一别的地方也没关系。感谢您的帮助提前!


解决方案

  perl的-ple的/ NUM / $ N ++ / E'文件名

试运行1,如果是这样做,你想要什么

I'm relatively new to scripting and apologize in advance for this painfully simple problem. I believe I've searched pretty thoroughly, but apparently no other answers or cookbooks have been explicit enough for me to understand (like here - still couldn't get it).

I have a file that is made up of strings of letters (DNA, if you care), one string per line. Above each string I've inserted another line to identify the underlying string. For those of you who are bioinformaticians, I'm trying to make up a test data set in fasta format, maybe you have tools? Anyway, I'd put a distinct word, "num", after each ">" with the intention of using a bash incrementer and sed to create a unique number heading each string. For example, in data.txt, I have...

>num, blah, blah, blah

ATCGACTGAATCGA

>num, blah, blah, blah

ATCGATCGATCGATCG

>num, blah, blah, blah

ATCGATCGATCGATCG

I would like it to be...

>0, blah, blah, blah

ATCGACTGAATCGA

>1, blah, blah, blah

ATCGATCGATCGATCG

>2, blah, blah, blah

ATCGATCGATCGATCG

The solution can be in any language as long as it's complete && gets the job done. I have a little experience with sed, awk, bash, and c++ (little == slightly more than no experience). I know, I know, I need to learn perl, but I've only just started. The question is this: How to replace "num" with a number that increments on each replacement? It doesn't matter if the underlying string is identical to another somewhere else. Thanks for your help in advance!

解决方案

perl -ple 's/num/$n++/e' filename

dry run 1st, if it is do that, what you want

这篇关于查找,替换,在绳子的occurence增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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