gpt4 book ai didi

linux - Bash 脚本 Unix。模式匹配

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:26 24 4
gpt4 key购买 nike

如何在没有空格的情况下在一行中编写多个字符串的脚本:

acgtttgggcccagctctccgccctcacacacaccccggggt

用于视觉目的:

acg ttt ggg ccc agc tct ccg ccc tca cac aca ccc cgg ggt

并且必须匹配重复 2 次的第 4 个 3 字母序列。所以在上面的序列中,我们将 ccc 作为第四个序列。并且在agc tct ccg之后再次重复。

那么我必须使用 grep 吗?

最佳答案

那么怎么样:

#!/bin/bash

# add a space every three letters
str="acgtttgggcccagctctccgccctcacacacaccccggggt"
result=$(sed -e 's/\(...\)/\1 /g' <<< "$str")
echo $result

# check if the 4th sequence is repeated two times
awk '
{ ref = $4; # set the 4th sequence as a reference
for (i=5; i<=NF; i++) # iterate from 5th sequence to the end
if (ref == $i) count++ # count the same one as the reference
printf "4th sequence \"%s\" repeated %d times.\n", ref, count
}' <<< "$result"

产生:

acg ttt ggg ccc agc tct ccg ccc tca cac aca ccc cgg ggt
4th sequence "ccc" repeated 2 times.

该脚本由两部分组成:第一个部分用于用空格分割字符串,第二个部分用于计算第四个三元组的重复次数。

  • sed 脚本 sed -e 's/\(...\)/\1/g' 在每三个字母后插入一个空格。
  • awk 脚本循环遍历与第四个三元组相同的序列。
  • 如果您只是想确保重复次数恰好是两次,您可以修改脚本以将count 与2 进行比较。

希望这有帮助。

关于linux - Bash 脚本 Unix。模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com