简体   繁体   English

如何在 Bash 中比较和递归修改字符串

[英]How to compare and recursively modify strings in Bash

I need to write a bash code performing some tasks I am going to explain.我需要编写一个 bash 代码来执行我将要解释的一些任务。 The input: two uppercase strings of same length, no matter their length is.输入:两个相同长度的大写字符串,无论它们的长度是多少。 Es:埃斯:

 CYVFGDDAS -->  string1   ,   unchangeable reference string
 CRFDGVEAT -->  string2   ,   modifiable string

I am trying to write Bash code that is able to compare the characters with same index recursively starting from the first position:我正在尝试编写能够从第一个位置开始递归比较具有相同索引的字符的 Bash 代码:

-- beginnig of the cycle --

if the characters are the same skip any action and go to the
  the next position, 
  
while
  
if the characters are not the same the character of string1
  replaces the character of string2 at that position
  
the new string2 is saved in a file

a substituion code is also written in the same file (I will 
explain this below)

the old string2 is replaced by the new string2 in such a way 
its changes are retained

start anothe cycle from the beginning

------

Repeat the cycle until the last character is processed.

So, for the example above, the code should start checking from the first position where two C characters are placed.因此,对于上面的示例,代码应该从放置两个 C 字符的第一个位置开始检查。 They match so no action is taken and both strings are left unchanged.它们匹配所以不采取任何行动并且两个字符串保持不变。

Going to he second position Y should replace R in the second string, the modified string should be saved and written in a text file togheter with the substitution code YA2V ( Y is the replacing character of string1, A is a costant character that must be present in all substitutions codes, 2 is the positional index where the substitution occurred, and V is the replaced character of string2).转到第二个位置 Y 应替换第二个字符串中的 R,修改后的字符串应保存并与替换代码YA2V一起写入文本文件中( Y是 string1 的替换字符, A是必须存在的共存字符在所有替换代码中, 2是替换发生的位置索引, V是string2的替换字符)。

I am proficient in Python which has a large number of modules for string manipulation but because the code should be added to a pre-existing Bash program I need to get this done in Bash environment (builtin commands, awk, sed etc, does not matter).我精通 Python,它具有大量用于字符串操作的模块,但是因为代码应该添加到预先存在的 Bash 程序中,所以我需要在 Bash 环境中完成此操作(内置命令、awk、sed 等,无关紧要) )。 Looks to me that Bash does not have an extended arsenal of tools like Python, so I am first of all wondering if this project is feasible or not.在我看来,Bash 没有像 Python 这样的扩展工具库,所以我首先想知道这个项目是否可行。

However, what I tried so far is to convert the strings in blank separated fields by inserting spaces between the characters in such a way awk can deal better with them as fields but I did not go very far with this.但是,到目前为止,我尝试的是通过在字符之间插入空格来转换空白分隔字段中的字符串,这样 awk 可以更好地将它们作为字段处理,但我对此并没有走得太远。 Sorry for the lengthy explanation.抱歉冗长的解释。 Any help is greatly appreciated.任何帮助是极大的赞赏。

No recursion is needed, just iterate over the strings.不需要递归,只需遍历字符串即可。 You can use parameter expansion with a for loop:您可以在 for 循环中使用参数扩展:

#!/bin/bash
s1=CYVFGDDAS
s2=CRFDGVEAT

for ((i=0; i<${#s1} ; ++i)) ; do
    if [[ ${s1:i:1} != ${s2:i:1} ]] ; then
        printf '%s\n' "${s1:0:i+1}${s2:i+1}"
        printf '%s\n' "${s1:i:1}A$((i+1))${s2:i:1}"
    fi
done

${s1:i:1} means extract the substring of $s1 from position $i of length 1 . ${s1:i:1} 表示从长度为 1 的$i位置提取$s1的子串 If the length is omitted, it extracts as much as it can.如果省略长度,它会尽可能多地提取。

It just outputs the strings, redirect them to files as you need.它只是输出字符串,根据需要将它们重定向到文件。

CYFDGVEAT
YA2R
CYVDGVEAT
VA3F
CYVFGVEAT
FA4D
CYVFGDEAT
DA6V
CYVFGDDAT
DA7E
CYVFGDDAS
SA9T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM