我希望能够从一行中提取两个不同的序列
我希望能够从一行中提取两个不同的序列.
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