0

我有一个长字符串文件 ( string.txt) ( ) 和一个像这样 abcdefghijklmnop的 vcf 表 ( )file.vcf

position 2 4 6 10 n...
name1 a b c d
name2 x y z a
namen...

该表还包含"mis"and"het"在这种情况下不应替换字符

我想更改特定位置的字符并将所有字符串存储在一个看起来像这样的新文件中

>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop

有没有办法在 bash 循环中做到这一点?

4

1 回答 1

1

请您尝试以下方法:

mapfile -t string < <(fold -w1 "string.txt")
# set string to an array of single characters: ("a" "b" "c" "d" ..)

while read -ra ary; do
    if [[ ${ary[0]} = "position" ]]; then
        # 1st line of file.vcf
        declare -a pos=("${ary[@]:1}")
        # now the array pos holds: (2 4 6 10 ..)
    else
        # 2nd line of file.vcf and after
        declare -a new=("${string[@]}")
        # make a copy of string to modify
        for ((i=0; i<${#pos[@]}; i++ )); do
            repl="${ary[$i+1]}"    # replacement
            if [[ $repl != "mis" && $repl != "het" ]]; then
                new[${pos[$i]}-1]="$repl"
                # modify the position with the replacement
            fi
        done
        echo ">${ary[0]}"
        (IFS=""; echo "${new[*]}")
        # print the modified array as a concatenated string
    fi
done < "file.vcf"

字符串.txt:

abcdefghijklmnop

文件.vcf:

position 2 4 6 10
name1 a b c d
name2 x y z a
name3 i mis k l

输出:

>name1
aacbecghidklmnop
>name2
axcyezghiaklmnop
>name3
aicdekghilklmnop

我已经尝试在上面的脚本中嵌入解释作为注释,但如果您仍有问题,请随时提问。

希望这可以帮助。

于 2019-11-23T13:21:03.083 回答