sed使用小记

本文介绍了如何使用sed命令从log.txt文件中提取特定字段值,包括name和num。通过示例展示了grep与sed结合使用的方式,以及在bash脚本中进行文件操作的流程。

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
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值