开发者

List of match offsets into a search string BASH only (modified by OP)

This file demonstrates a typical @installhook

it does exactly the same thin开发者_如何学编程g as if you would have called apps/install GoFukUrself

and this file did not exist.

This system exists so we can copy multiple files, rather than just a single file if

need be. Your file does not have to be a shell file, it can be any script or program.


There is:

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

uniq -c is the important bit here, which is what's counting the instances which appear on separate lines in its input. sort is required by uniq. tr splits the input so there's only one "word" per line.

EDIT: I may have misinterpreted. This gives you the count of each match, which is what the awk example you've given gives you.


I don't know what you mean by a "full match" or where you're counting your indices from, but I think you're maybe looking for the match function:

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


# 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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜