答案 0 :(得分:3)
有:
echo "1,1,2,5,5,5,6,5,4,5,7" | tr ',' '\n' | sort | uniq -c
uniq -c
在这里是重要的一点,它正在计算出现在输入中不同行上的实例。 uniq需要sort
。 tr
分割输入,因此每行只有一个“单词”。
答案 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
}