我希望能够从一行中提取两个不同的序列 [英] I want to be able to extract two different sequences from one line

查看:52
本文介绍了我希望能够从一行中提取两个不同的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从一行中提取两个不同的序列.

I want to be able to extract two different sequences from one line.

例如:

atg ttg tca aat tca tgg atc atg ttg tca aat tca tgg atc 标签

atg ttg tca aat tca tgg atc atg ttg tca aat tca tgg atc tag

我想创建一个循环,程序将从第一个 atg 读取到 tag ,将该序列输出到文件中,并获取第二个 atg 读取到标签,然后将该序列输出到同一文件中.

I want to create a loop where the program will read from the 1st atg to tag, output that sequence into a file, as well as take the second atg read to tag, output that sequence into the same file.

我想要的输出:

atg ttg tca aat tca tgg atc atg ttg tca aat tca tgg atc tag
atg ttg tca aat tca tgg atc tag

我该如何处理?

谢谢您的帮助.

推荐答案

当您最多需要2个序列时,可以在原始字符串和修改后的字符串中 grep :

When you want at most 2 sequences, you can grep inside the original and a modified string:

s='atg ttg tca aat tca tgg atc atg ttg tca aat tca tgg atc tag'
printf "%s\n" "$s" "${s#*atg}" | grep -Eo "atg.*tag"

如果要在可用时提取两个以上的子字符串,则需要循环.

When you want to extract more than 2 substrings when available, you need a loop.

s='atg ttg tca aat tca tgg atc atg ttg tca aat tca tgg atc tag'
while [ "$s" ]; do
   s=$(grep -Eo "atg.*tag" <<< "$s")
   if [ "$s" ]; then
      echo "$s"
      s="${s#atg}"
   fi
done

这篇关于我希望能够从一行中提取两个不同的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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