当{Curly Braces}扩展的排列太多时

发布于 2025-01-23 04:06:44 字数 305 浏览 4 评论 0原文

我知道卷曲括号的扩展是由外壳来处理的,而不是命令,例如回声。当排列太多时,进入命令之前需要太长的内存时间太长。是否有可能让第一个排列馈入命令,然后将第二个排列置于命令中,依此类推。这是因为我在命令中有一个检查条件,因此如果某些排列匹配,则将停止。

echo {a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}

可以在shell脚本中通过许多循环来完成。但是,如果有一定的语法允许相同的行为,那将很方便。

I know that the curly braces expansion is handled by the shell, not the command e.g. echo. When there are too many permutations, it takes too long as well as too many memory BEFORE entering the command. Is it possible to let the first permutation feed into the command, then the second permutation, and so on. It is because I have a check condition in the command, such that if certain permutation matches, it will be stopped.

echo {a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}

It can be done by many for loops in a shell script. But it would be convenient if there are certain syntax allow the same behaviour.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-01-30 04:06:44

这是通过使用给定的卷曲括号来生成用递归生成排列的想法。

#!/bin/bash

str="{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}"

# function to generate permutations
perm() {
    local s1=$1
    local s2=$2
    local i
    local -a ary m

    if [[ $s1 =~ \{([^}]+)}(.*) ]]; then        # match leftmost pair of curly braces
        m=( "${BASH_REMATCH[@]}" )              # backup the capture group
        IFS=, read -ra ary <<< "${m[1]}"        # split the string on commas into array "ary"
        [[ ${m[1]} =~ ,$ ]] && ary+=( "" )      # append empty element if the string ends with comma
        for i in "${ary[@]}"; do                # loop over the array "ary"
            if [[ -n ${m[2]} ]]; then           # if other curly braces remain on the right
                perm "${m[2]}" "$s2$i"          # then recursivery call the function
            elif [[ -n $s2$i ]]; then           # end of the recursion: skip empty value which appears just once
                echo "$s2$i"
                # do your operation here with "$s2$i"
            fi
        done
    fi
}

perm "$str"

尽管它立即开始产生排列,但要比Bash的支架扩展可能需要更长的时间才能完成。如果这在执行时间不实用,我将使用其他语言(例如Perl)更新我的答案。

Here is an idea to generate the permutations with recursion by using the given string of curly braces.

#!/bin/bash

str="{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}{a,b,c,d,}"

# function to generate permutations
perm() {
    local s1=$1
    local s2=$2
    local i
    local -a ary m

    if [[ $s1 =~ \{([^}]+)}(.*) ]]; then        # match leftmost pair of curly braces
        m=( "${BASH_REMATCH[@]}" )              # backup the capture group
        IFS=, read -ra ary <<< "${m[1]}"        # split the string on commas into array "ary"
        [[ ${m[1]} =~ ,$ ]] && ary+=( "" )      # append empty element if the string ends with comma
        for i in "${ary[@]}"; do                # loop over the array "ary"
            if [[ -n ${m[2]} ]]; then           # if other curly braces remain on the right
                perm "${m[2]}" "$s2$i"          # then recursivery call the function
            elif [[ -n $s2$i ]]; then           # end of the recursion: skip empty value which appears just once
                echo "$s2$i"
                # do your operation here with "$s2$i"
            fi
        done
    fi
}

perm "$str"

Although it instantly starts to generate the permutations, it may take a longer time to complete than the brace expansion of bash. If this is not practical in execution time, I'll update my answer with more efficient code using other languages such as perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文