匹配偏移列表仅限于搜索字符串BASH(由OP修改)

时间:2011-05-20 14:39:56

标签: bash shell

此文件演示了典型的@installhook

它与您调用apps / install GoFukUrself

完全相同

,此文件不存在。

这个系统存在,所以我们可以复制多个文件,而不是只复制一个文件

需要。您的文件不必是shell文件,它可以是任何脚本或程序。

3 个答案:

答案 0 :(得分:3)

有:

echo "1,1,2,5,5,5,6,5,4,5,7" | tr ',' '\n' | sort | uniq -c

uniq -c在这里是重要的一点,它正在计算出现在输入中不同行上的实例。 uniq需要sorttr分割输入,因此每行只有一个“单词”。

编辑:我可能误解了。这为您提供了每场比赛的计数,这就是您给出的awk示例。

答案 1 :(得分:2)

我不知道你所说的“完全匹配”是什么意思,或者你在计算你的指数的地方,但我认为你可能正在寻找match函数:

   match(s, r [, a])       Returns the position in  s  where  the  regular
                           expression  r occurs, or 0 if r is not present

答案 2 :(得分:1)

# where: [$] = NAME of a string var, [%] IMMEDIATE VALUE

# example:
# declare container lookfor; declare -i offset;
# .
# .
# string.find container lookfor $offset;

shopt -s extglob    # activate extended regular expression parsing.

declare result;

# offset is optional. -% = undef.
function string.find { : [$]source [$]find [%]offset

    local buffer=${!1} find=${!2} empty='';
    local -i offset=${3:-0};

    [[ $offset -eq 0 ]] || buffer=${buffer:$offset};

    [[ -n "$buffer" ]] || { result=$empty; return 1; }

    #  Matches at front of string?
    [[ "$buffer" =~ ^("$find") ]] && { result=0; return 1; }

    [[ "$buffer" =~ ^(.*|$)?("$find")(.+|$) ]] && {

        let buffer=${#BASH_REMATCH[1]}+$offset;

    } || {

        result=$empty; return 1;

    }

    result=$buffer;

}

function string.find.all { : [$]source [$]find

    local source=${!1} find=${!2} foundlist='' offset=0;

    while string.find source find $offset; do

        foundlist+="$result ";
        let offset=$result+${#find};

    done

    echo $foundlist

}