改变字符串bash中字符的位置

时间:2014-08-28 07:29:09

标签: string bash

我有这个字符串

E="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

如果用户输入

,任何想法如何改变字母与其邻居的位置

并且它将继续改变位置,直到用户对字符串满意或者它已到达字符串的末尾。

is the position of 1st correct? Y/N
N
E=BACDEFGHIJKLMNOPQRSTUVWXYZ
*some of my code here*
are u satisfied? Y/N
N

is the position of 2nd correct? Y/N
N
E=BCADEFGHIJKLMNOPQRSTUVWXYZ
*some of my code here*
are u satisfied? Y/N
N

is the position 3rd correct? Y?N
Y
E=BCADEFGHIJKLMNOPQRSTUVWXYZ
*some of my code here*
are u satisfied? Y/N
N

is the position 4th correct? Y?N
Y
E=BCADEFGHIJKLMNOPQRSTUVWXYZ
*some of my code here*
are u satisfied? Y/N
Y

*exit prog*

任何帮助将非常感谢。感谢

编辑 我从一个论坛得到了这个代码。工作得很好。但是任何想法如何在它完成一次之后交换下一个字符?例如我已经完成了第一个位置,我想为第二个角色运行它?任何想法?

dual=ETAOINSHRDLCUMWFGYPBVKJXQZ
phrase='E'
rotat=1
newphrase=$(echo $phrase | tr "${dual:0:26}" "${dual:${rotat}:26}")
echo ${newphrase}

3 个答案:

答案 0 :(得分:3)

你必须使用循环。

#!/bin/bash

E="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo "$E"

for (( i = 1; i < ${#E}; i++ )); do
  echo "Is position $i correct? Y/N"
  read answer

  if [ "$answer" == "N" -o "$answer" == "n" ]
  then
    E="${E:0:$i-1}${E:$i:1}${E:$i-1:1}${E:$i+1}"
  fi

  echo "$E"
  echo "Are you satisfied? Y/N"
  read answer

  if [ "$answer" == "Y" -o "$answer" == "y" ]
  then
    break
  fi
done

循环遍历字符串的每个字符。字符串更改发生在第一个if子句中。它只不过是基本的子串操作。 ${E:n}从位置E开始返回n的子字符串。从${E:n:m}开始,m会返回E的下一个n个字符。如果用户满意并想要退出,剩下的行就是处理。

答案 1 :(得分:1)

使用bash,您可以轻松提取子字符串:

${string:position:length}

此语法允许您使用变量扩展,因此在字符串中交换两个连续字符非常简单:

E="${dual:0:$rotat}${dual:$((rotat+1)):1}${dual:$rotat:1}${dual:$((rotat+2))}"

Arithmetics可能需要加入$((...))

答案 2 :(得分:0)

来自bash手册页:

   ${parameter:offset}
   ${parameter:offset:length}
          Substring Expansion.  Expands to up to length characters of parameter starting at  the  character  specified  by  offset.   If
          length is omitted, expands to the substring of parameter starting at the character specified by offset.  length and offset are
          arithmetic expressions (see ARITHMETIC EVALUATION below).  length must evaluate to a number greater than or equal to zero.  If
          offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter.  If param-
          eter is @, the result is length positional parameters beginning at offset.  If parameter is an array name indexed by @  or  *,
          the result is the length members of the array beginning with ${parameter[offset]}.  A negative offset is taken relative to one
          greater than the maximum index of the specified array.  Note that a negative offset must be separated from  the  colon  by  at
          least one space to avoid being confused with the :- expansion.  Substring indexing is zero-based unless the positional parame-
          ters are used, in which case the indexing starts at 1.

示例:

pos=5
E="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo "${E:pos:1}  # print 6th character (F)
echo "${E:pos}    # print from 6th character (F) to the end

当你说&#34;它的邻居&#34;你是什么意思?除了第一个和最后一个字符,字符串中的每个字符都有两个邻居。

交换&#34; POS&#34;字符(从1开始)和下一个字符(POS + 1):

E="${E:0:POS-1}${E:POS:1}${E:POS-1:1}${E:POS+1}"