
このような文字列があります。
data = "state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 2 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 2 caller_contact:: sip:[email protected]:5080;transport=udp
state:: 1 caller_contact:: sip:[email protected]:5080;transport=udp"
各IP(たとえば、192.168.26)にステータス4またはステータス2がある回数を計算するには、bashスクリプトを作成する必要があります。 (文字列に「/n」は含まれていません)
文字列を解析して各IPに基づいて値を計算することはできません。
答え1
可能なすべての組み合わせでこれが機能するかどうかはわかりませんが、提供した小さなサンプルでは機能します。
sed "1,\$s/state/\nstate/g" file | grep state > NewFile
for IPADDR in $(cat NewFile | cut -d"@" -f2|cut -d":" -f1|sort -n|uniq);do
for STATE in 2 4 ;do
LineCount=$(grep "${IPADDR}" NewFile |grep "state:: ${STATE}"| wc -l)
echo "For IP address ${IPADDR}, status:: ${STATE} lines count is ${LineCount}"
done
done | grep -v "is 0"$
forループに必要に応じてさまざまなSTATE番号を追加できます。
デフォルトでは、文字列が現れるたびに改行文字を挿入してstate
大きなデータブロックを形成し、複数行に分割します。
答え2
一意のIPを見つけ、各状態を4または2として計算します。
for addr in $(grep -o '@[^:]\+' file | sort -u); do
echo -n ${addr#@}:\
grep -c ":: [24].*${addr#@}" file
done
または、awkを使用して操作を実行してください。
awk -F '[: @;=]+' '
$2 ~ /^[24]$/{
count[$6]++
}
END {
for(i in count)
print(i, count[i])
}
' file
答え3
これは私の小さなモンスターです。
#!/bin/bash
# monsterr.sh
if [ -z "$1" ] ; then
printf "%s\n" "Syntax error."
exit 2
fi
data="$1"
tmp=tmp$$
mkdir $tmp
parse() {
state=
ip=
i=0
while read l; do
((i++))
if [ $(($i%2)) -eq 0 ]; then
if [ $(($i%4)) -eq 2 ]; then
state=$l
else
IFS=: read x ip x < <(printf %s $l)
IFS=@ read x ip < <(printf %s $ip)
printf "%s\n" $state >> "$tmp/$ip"
fi
fi
done < <(printf "%s\n" $data)
}
report() {
cd $tmp
for f in * ; do
declare -a count
printf "IP: %s\n" $f
while read s ; do
((count[$s]++))
done < $f
for s in ${!count[@]}; do
printf "State: %s, count: %s\n" $s ${count[$s]}
done
printf '\n'
unset count
done
cd - 2 > /dev/null
}
parse
report
rm -r $tmp
使用法:
$ data="state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp state:: 4 caller_contact:: sip:[email protected]:5080;transport=udp state:: 2 caller_contact:: sip:[email protected]:5080;transport=udp state:: 2 caller_contact:: sip:[email protected]:5080;transport=udp state:: 1 caller_contact:: sip:[email protected]:5080;transport=udp"
$ ./monsterr.sh "$data"
IP: 192.168.10.01
State: 4, count: 1
IP: 192.168.10.03
State: 4, count: 1
IP: 192.168.10.07
State: 1, count: 1
IP: 192.168.10.11
State: 2, count: 1
State: 4, count: 1
IP: 192.168.10.26
State: 2, count: 1
State: 4, count: 1
答え4
grep
処理したい行に対してsed
.Use sed
、sort、およびcountを使用して、行から2つの部分文字列を取得できます。
sed -n '/state:: [24] .*@/ s/.*:: \([24]\)[^@]*@\([^:]*\).*/State \1 IP \2/p' file |
sort | uniq -c | sort -n