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

标签 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 word3 word4 word5
word3 word4 word5 word6
word1 word2 word3 word4 word5
word2 word3 word4 word5 word6
word1 word2 word3 word4 word5 word6

由于知识有限,我以前都是一行一行地这样做,但如果该行包含很多单词,我就必须输入数百行。所以请帮助我。

最佳答案

您要求“给定字符串的所有子字符串,不拆分单词«或”数组的所有子数组«。

bash 解决方案

#! /bin/bash

array=(word1 word2 word3 word4 word5 word6)

for (( length=1; length <= "${#array[@]}"; ++length )); do
    for (( start=0; start + length <= "${#array[@]}"; ++start )); do
        echo "${array[@]:start:length}"
    done
done

sh 解决方案

由于 mklement0 更好而被删除 sh-solution .

关于bash - 在bash中,如何生成所有可能的单词组合,但按原始顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43384770/

相关文章:

linux - 这行代码在 shell 中是如何工作的?

linux - bash for循环获取文件内容写入另一个文本文件

python - 如何使用 Python 创建 JKS 或 P12 keystore

bash - 如何判断 bash 脚本中的任何命令是否失败(非零退出状态)

linux - 按时间顺序捕获 STDOUT 和 STDERR

html - 将 Shell 脚本变量输出为 HTML

bash - 在 shell 脚本中正确使用 bc 吗?

shell 脚本 : Count number of files in a particular type extension in single folder

shell - 如何 printf 像表格一样对齐我的输出?

unix - Perl : Shebang (space? ) "#! "?