Один из способов сделать это:
#! /bin/sh
n=$1
m=$2
# Reference: http://math.stackexchange.com/q/48080
sum=$(( (m * (m + 1) * (2 * m + 1) - (n - 1) * n * (2 * n - 1)) / 6 ))
echo $sum
Результат:
./sum 3 7
135
datamash -t: groupby 1 collapse 2 < file
A:1,2,3
B:a,b
C:pp
D:rr
Если вам нужны подсчеты,
datamash -t: groupby 1 collapse 2 count 2 < file
A:1,2,3:3
B:a,b:2
C:pp:1
D:rr:1
Вы также можете countunique
, если вам нужно количество уникальных полей.
это задание для awk.
awk -F: '{L[$1]=L[$1] "," $2}
END { for (l in L) printf "%s:%s\n",l,substr(L[l],2);}'
где
-F:
использовать :
в качестве разделителя {L [$ 1] = L [$ 1] "," $ 2}
сохранить значение, разделенное запятыми, индексированное поле 1 END
в конце файла for (l in L)
цикл значений printf "% s:% s \ n", l, substr (L [l] , 2);
печать, пропуская первую запятую ","
или ","
, соответственно отрегулируйте конечную подстроку. awk может быть однострочным, используйте
awk -F: '....' File1 > File3
для подсчета генов, просто добавьте количество переменных (здесь G).
{L[$1]=L[$1] "," $2;G[$1]++}
END { for (l in L) printf "%s:%s:%d\n",l,substr(L[l],2),G[l];}
%h = (
...
B => [a, b],
A => [1, 2, 3],
...
);
perl -F':' -lane '
push @{$h{$F[0]}}, $F[1]}{
$"=",";
print "$_:", "@{$h{$_}}|", scalar @{$h{$_}} for keys %h;
' File1 > File1.new
The field separator is set to a semicolon, thus populating each time a line is read in afresh
the @F array. Then we append the 2nd field, $F[1], to the array of hash
keyed in on the 1st field, $F[0]. At the end, we display the key name,
followed by the array contents corresponding to this key, & the count of
the array as well.
A:1,2,3|3
B:a,b|2
C:pp|1
D:rr|1
sed -e '
:loop
$!N
s/^\(\([^:]*\):.*\)\n\2:\(.*\)/\1,\3/
tloop
P;D
' yourfile