log.txt文件内容如下:
num:[zhangsan],name:[lisi]
num:[wangwu],name:[liuyi]
1、提取name字段值
grep -nr yourname log.txt|sed -r 's/.*name:\[(.*)\]/\1/'
执行结果如下:
lisi
liuyi
2、提取num字段值
grep -nr yourname log.txt|sed -r 's/.*num:\[(.*)\],.*/\1/'
执行结果如下
zhangsan
wangwu
#!/usr/bin/env bash
cur_dir=$(
cd "$(dirname "$0")" || exit 1
pwd
)
if [ ! -d "$cur_dir/scu_data" ]; then
mkdir "$cur_dir/scu_data"
fi
today_date=$1
scu_log_dir=$2
time_hour=$3
declare -A called_num
declare -A file_already_read
get_data() {
date=$1
log_date=$2
today=$(ls $scu_log_dir | grep scu | grep -A 1 $date)
if [ -z "$today" ]; then
echo "can't found $date log file!"
exit 1
fi
IFS=$'\n\n'
for file in $(echo -e "$today"); do
for num in $(grep -E"$log_date.*IAM |$log_date.*send IAM" $scu_log_dir/$file); do
aaaaa=$(echo $num|cut -d '.' -f 2|cut -d '.' -f 1)
bbbbb=$(echo $num|cut -d ' ' -f 10|sed -r 's/\[(.*)\],/\1/')
echo "aaaaa=$aaaaa----bbbbb=$bbbbb" >> iam.txt
done
done
for file1 in $(echo -e "$today"); do
for num in $(grep -E"$log_date.* REL" $scu_log_dir/$file1); do
aaaaa=$(echo $num|cut -d '.' -f 2|cut -d '.' -f 1)
bbbbb=$(echo $num|cut -d ' ' -f 10|sed -r 's/\[(.*)\]/\1/')
echo "aaaaa=$aaaaa----bbbbb=$bbbbb" >> rel.txt
done
done
for file1 in $(echo -e "$today"); do
for num in $(grep -E"$log_date.*send REL" $scu_log_dir/$file1); do
aaaaa=$(echo $num|cut -d '.' -f 2|cut -d '.' -f 1)
bbbbb=$(echo $num|cut -d ' ' -f 9|sed -r 's/\[(.*)\]/\1/')
echo "aaaaa=$aaaaa----bbbbb=$bbbbb" >> rel.txt
done
done
}
get_time(){
for file1 in $(cat iam.txt); do
pid=$(echo $file1|cut -d '-' -f 4)
start_time=$(echo $file1|cut -d '-' -f 2)
num=$(grep $pid rel.txt)
end_time=$($num | cut -d '-' -f 2)
a1=$(($(date +%s -d $start_time) - $(date +%s -d $end_time)))
done
}
main() {
if [ ! -z $today_date ]; then
today_log_date="${today_date:0:4}-${today_date:4:2}-${today_date:6:2} $time_hour"
echo "today_log_date = $today_log_date"
get_data "$today_date" "$today_log_date"
echo "Get $today_date done!"
exit 0
fi
}
main