使用 -v 选项反转 grep 未按预期工作
我有以下脚本,它检查备份日志中是否有错误,并希望它打印错误代码之前的行中的错误信息,而不打印错误代码。我尝试了这个解决方案,但我仍然得到这两行。
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要解决您提出的问题,您可以替换此管道:
使用这个命令(使用 GNU awk 进行 IGNORECASE,也可以轻松调整其他 awk 版本):
但实际上,您的大部分脚本可能应该替换为单个命令调用 awk,例如:
但是我们需要有关 bck.log 布局、预期输出以及几行示例输入/输出的更多信息来真正帮助您,因此如果您愿意,请提出一个新问题更多帮助。
To solve the problem you asked about you could replace this pipeline:
with this single command (using GNU awk for IGNORECASE, easy to tweak for other awk versions too):
In reality, though, most of your script should probably be replaced with a single call to awk such as:
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.