使用 -v 选项反转 grep 未按预期工作

发布于 2025-01-10 20:05:10 字数 1072 浏览 0 评论 0原文

我有以下脚本,它检查备份日志中是否有错误,并希望它打印错误代码之前的行中的错误信息,而不打印错误代码。我尝试了这个解决方案,但我仍然得到这两行。

#!/bin/bash
mytail=$(tail -n 13 bck.log)
mydate=$(date -dyesterday +%y%m%d)
bckerr=$(grep -B1 -i "backupexitcode:" bck.log | grep -v "backupexitcode:")

if [[ $mytail =~ $mydate ]]
then
        echo "Backup is up to date!"
        mytail=${mytail,,}
        if [[ "$mytail" == *failed* || "$mytail" != *backupexitcode:0* ]]
        then
                echo "There is an error in the last backup!"
                echo "$bckerr"
                        exit 1001
        else
                echo "No errors were found."
                        exit 0
        fi
else
        echo "Backup didn't run!"
                exit 1002
fi

所涉及的两行与其余行分开:

INFO         Older backups were deleted without error.
run_backup.sh(25863) 220228-200357: Instance:server:port Schema:"_instanceBackup" Finish: Timestamp:220228_200236 BackupExitCode:0

我见过一些使用 sed 或 awk 解决的类似问题,但这些问题都不适合我。

I have the following script which checks backup logs for errors and want it to print the error information that is standing in the line before the errorcode without also printing the errorcode. I tried this solution but I still get both lines.

#!/bin/bash
mytail=$(tail -n 13 bck.log)
mydate=$(date -dyesterday +%y%m%d)
bckerr=$(grep -B1 -i "backupexitcode:" bck.log | grep -v "backupexitcode:")

if [[ $mytail =~ $mydate ]]
then
        echo "Backup is up to date!"
        mytail=${mytail,,}
        if [[ "$mytail" == *failed* || "$mytail" != *backupexitcode:0* ]]
        then
                echo "There is an error in the last backup!"
                echo "$bckerr"
                        exit 1001
        else
                echo "No errors were found."
                        exit 0
        fi
else
        echo "Backup didn't run!"
                exit 1002
fi

The 2 lines that are involved seperated from the rest:

INFO         Older backups were deleted without error.
run_backup.sh(25863) 220228-200357: Instance:server:port Schema:"_instanceBackup" Finish: Timestamp:220228_200236 BackupExitCode:0

I've seen a few similar problems that were solved using sed or awk but none of those worked for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2025-01-17 20:05:10

要解决您提出的问题,您可以替换此管道:

bckerr=$(grep -B1 -i "backupexitcode:" bck.log | grep -v "backupexitcode:")

使用这个命令(使用 GNU awk 进行 IGNORECASE,也可以轻松调整其他 awk 版本):

bckerr=$(awk -v IGNORECASE=1 '/backupexitcode/{print prev} {prev=$0}' bck.log)

但实际上,您的大部分脚本可能应该替换为单个命令调用 awk,例如:

$ cat tst.sh
#!/usr/bin/env bash

awk -v IGNORECASE=1 -v mydate="$(date -d yesterday +'%y%m%d')" '
BEGIN {
    msgs[0]    = "No errors were found."
    msgs[1001] = "There is an error in the last backup!"
    msgs[1002] = "Backup didn\047t run!"
    status = 1002
}
$0 ~ mydate {
    if ( /failed|backupexitcode:[1-9]/ ) {
        status = 1001
        msgs[status] = msgs[status] ORS prev
        exit
    }
    else {
        status = 0
    }
    prev = $0
}
END {
    print msgs[status] > "/dev/stderr"
    exit status
}
' 'bck.log'

但是我们需要有关 bck.log 布局、预期输出以及几行示例输入/输出的更多信息来真正帮助您,因此如果您愿意,请提出一个新问题更多帮助。

To solve the problem you asked about you could replace this pipeline:

bckerr=$(grep -B1 -i "backupexitcode:" bck.log | grep -v "backupexitcode:")

with this single command (using GNU awk for IGNORECASE, easy to tweak for other awk versions too):

bckerr=$(awk -v IGNORECASE=1 '/backupexitcode/{print prev} {prev=$0}' bck.log)

In reality, though, most of your script should probably be replaced with a single call to awk such as:

$ cat tst.sh
#!/usr/bin/env bash

awk -v IGNORECASE=1 -v mydate="$(date -d yesterday +'%y%m%d')" '
BEGIN {
    msgs[0]    = "No errors were found."
    msgs[1001] = "There is an error in the last backup!"
    msgs[1002] = "Backup didn\047t run!"
    status = 1002
}
$0 ~ mydate {
    if ( /failed|backupexitcode:[1-9]/ ) {
        status = 1001
        msgs[status] = msgs[status] ORS prev
        exit
    }
    else {
        status = 0
    }
    prev = $0
}
END {
    print msgs[status] > "/dev/stderr"
    exit status
}
' 'bck.log'

but we'd need more information on the layout of bck.log, the expected output, and a few lines of sample input/output to really help you with that so ask a new question if you'd like more help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文