同事需要对大的日志进行分析统计,但又不太懂linux脚本,所以做了个按选择的列进行统计数量并排序的脚本
此脚本会先显示样本内容,以及对样本内容进行分列,展示每列的内容,并根据输入的列数,进行排序
#!/bin/bash
usage(){
echo "Usage: ./1.sh filename or sh 1.sh filename"
echo "Required Parameters:filename"
echo "Example:sh 1.sh smb.txt"
exit 1
}
if [ $# -eq 0 ];then
echo "缺少参数";
usage;
fi
echo "下面计算总共有正常匹配数据的列数"
num=`cat $1|tail -n 10|grep "/"|head -n 1|awk '{print NF}'`
echo "总共$num列"
echo "样本内容为:"
echo `cat $1|tail -n 10|grep "/"|head -n 1`
echo "下面开始查看每列的内容"
echo -n `cat $1|tail -n 10|grep "/"|head -n 1`|awk '{gsub(/ /,"\n"); for(i=1;i<=NF;i++) print i ": " $i}'
echo ""
echo ""
read -p "请输入你要选择统计的列: " num1
read -p "请确认你选择统计的列: " num2
if [ $num1 = $num2 ]; then
echo "你要统计的为第$num1列,下面马上为您呈现TOP20"
cat $1|awk -v col=$num1 '{print $col}'|sort |uniq -c|sort -nk 1|tail -n 20
else echo "两次输入有误,请重新执行脚本"
fi