在bash中,如何生成所有可能的单词组合,但要按原始顺序?

在bash中,如何生成所有可能的单词组合,但要按原始顺序?,bash,sh,Bash,Sh,从这一行: word1 word2 word3 word4 word5 word6 如何返回以下内容: word1 word2 word3 word4 word5 word6 word1 word2 word2 word3 word3 word4 word4 word5 word5 word6 word1 word2 word3 word2 word3 word4 word3 word4 word5 word4 word5 word6 word1 word2 word3 word4 word2

从这一行:

word1 word2 word3 word4 word5 word6
如何返回以下内容:

word1
word2
word3
word4
word5
word6
word1 word2
word2 word3
word3 word4
word4 word5
word5 word6
word1 word2 word3
word2 word3 word4
word3 word4 word5
word4 word5 word6
word1 word2 word3 word4
word2 word3 word4 word5
word3 word4 word5 word6
word1 word2 word3 word4 word5
word2 word3 word4 word5 word6
word1 word2 word3 word4 word5 word6

由于知识有限,我过去常常一行一行地写,但如果一行包含很多单词,我就得打几百行。所以请帮帮我。

我不知道你需要什么样的突变,因为你所展示的不是排列。但如果需要排列,可以使用外壳扩展:

echo {1..6}{1..6}{1..6}{1..6}{1..6}{1..6}
我省略了“单词”字符串和新行

使用
{word1,word2,word3,word4,word5,word6}
查找单词。

您要求的是»给定字符串的所有子字符串,而不拆分单词«或»数组的所有子数组«

bash解决方案
#/bin/bash
数组=(word1 word2 word3 word4 word5 word6)
for((length=1;length提供了一个优雅的
bash
解决方案

用一个可移植的、高效的
sh
实现对其进行补充,该实现还可以处理输入字之间不同数量的空格(和/或制表符):

#!/bin/sh

# Input string.
string='word1 word2 word3 word4 word5 word6'

# Compress runs of multiple spaces (and/or tabs) between words, if any,
# to a single space.
string=$(printf '%s\n' "$string" | tr -s '[:blank:]' ' ')

# Count the number of words in the string.
# Note: On BSD/macOS, the result will have leading whitespace,
#       but with operators such as `-le` inside `[ ... ]` and with
#       *unquoted* references inside $(( ... )) that is not a problem.
wordCount=$(printf '%s\n' "$string" | wc -w)

start=1
while [ $start -le $wordCount ]; do
  end=$start
  while [ $end -le $wordCount ]; do
    printf '%s\n' "$string" | cut -d ' ' -f "$start-$end"
    end=$(( end + 1 ))
  done
  start=$(( start + 1 ))
done

[1] 在
bash
中,只要根据需要对元素应用引号,将字符串拆分为具有文字的数组元素是安全的:
因此,
array=(word1 word2 word3 word4 word5 word6)
工作稳定(元素不需要引用),但是
array=($str)
通常不稳定,因为
$str
扩展到的内容会受到全局化的影响(以及分词,但这在这里是需要的)。将单行字符串中以空格分隔的标记读入数组的安全方法是:
read-ra array使用
python
对于这种情况,您需要查看字符串中以规范排序顺序排列的单词。(注意:不是单词中字符的排列)。可以找到许多示例。(如果你想快速,请使用C,bash并不完全适合这种情况——它可以工作,只是效率不高)我在终端仿真器中使用android中的脚本。因此可能没有phyton或C或其他脚本语言。但是如何在phyton/C中实现这一点呢?非常感谢任何帮助。在bash中也是可能的……1)2)3)找出嵌套循环逻辑以获得给定的所需输出4)或
回送字{1..6}”“…”字{1..6}$'\n'
。但即使这样,这也只会生成所有置换,其中大部分不会保留原始顺序。我的
sh
脚本运行速度比这快3秒。@user163125:我很高兴听到这个消息-在shell循环中,无论您是否在每次迭代中调用外部实用程序,都会产生很大的差异。
#!/bin/sh

# Input string.
string='word1 word2 word3 word4 word5 word6'

# Compress runs of multiple spaces (and/or tabs) between words, if any,
# to a single space.
string=$(printf '%s\n' "$string" | tr -s '[:blank:]' ' ')

# Count the number of words in the string.
# Note: On BSD/macOS, the result will have leading whitespace,
#       but with operators such as `-le` inside `[ ... ]` and with
#       *unquoted* references inside $(( ... )) that is not a problem.
wordCount=$(printf '%s\n' "$string" | wc -w)

start=1
while [ $start -le $wordCount ]; do
  end=$start
  while [ $end -le $wordCount ]; do
    printf '%s\n' "$string" | cut -d ' ' -f "$start-$end"
    end=$(( end + 1 ))
  done
  start=$(( start + 1 ))
done