Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sed的p是打印,d是删除,取反! #92

Open
vieyahn2017 opened this issue Jan 27, 2024 · 4 comments
Open

sed的p是打印,d是删除,取反! #92

vieyahn2017 opened this issue Jan 27, 2024 · 4 comments

Comments

@vieyahn2017
Copy link
Owner

sed的p是打印,d是删除,取反!

@vieyahn2017
Copy link
Owner Author

一个取日志的脚本

日志格式

Sat Jan 27 05:42:24 PM CST 2024  (date命令执行结果)
===================$FUNCNAME================
PARAM=xxx
1
2
3
4
5
6  inode 123456
7
8
9
a
b
run [ xxx ] to test.

@vieyahn2017
Copy link
Owner Author

vieyahn2017 commented Jan 27, 2024

function get_last_inode_log()
{
    if [ -z $1 ]; then
        if [ $(cat $LOG_FILE | tail -n 20 |grep -c "M CST") -eq 0 ]; then
            cat $LOG_FILE | tail -n 20
        else
            cat $LOG_FILE | tail -n 20 | sed '1,/M CST/d'
        fi
        return
    fi
    local inode=$1
    local first_no=$(cat $LOG_FILE | grep -n $inode | awk -F":" 'NR==1{print $1}')
    local first_no_before_10=$((first_no-10))
    sed -n ${first_no_before_10},${first_no}'p' $LOG_FILE | sed '1,/M CST/d'
    local last_no=$(cat $LOG_FILE | grep -n $inode | awk -F":" 'END{print $1}')
    local last_no_after_20=$((last_no+20))
    sed -n $(($first_no+1)),$((last_no-1))'p' $LOG_FILE
    sed -n ${last_no},${last_no_after_20}'p' $LOG_FILE | sed '1,/run \[/!d'
}

@vieyahn2017
Copy link
Owner Author

代码写的很丑

cat $LOG_FILE | grep -n $inode | awk -F":" '{print $1}'
410
411
412
413
414
415
416
417
418

first_no=410 往前找10行,然后删掉匹配到的date行之前的,sed '1,/PM CST/d'
这之间用sed的p输出411-417行
last_no=418往后找20行,删掉run [ xxx ] 之后的,先用sed '1,/run [/d',然后加了个取反的!

@vieyahn2017
Copy link
Owner Author

不知道有没有更好的实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant