使用 grep 或 ack 可靠地检查 wget 日志中的错误
Reliably checking wget log for errors using grep or ack
在 bash 文件中,我有 logfileA.txt
包含来自 wget
的输出,我想 运行 grep
检查“错误”或“失败”等词的任何实例,如下所示:
grep -ni --color=never -e "error" -e "fail" logfileA.txt | awk -F: '{print "Line "": "}'
# grep -n line number, -i ignore case; awk to add better format to the line numbers (
但问题是,我认为 logfileA.txt
中的 wget
输出充满了字符,可能会弄乱 grep
的输入,因为我没有得到可靠的匹配.
解决此问题时,我什至无法 cat
可靠地查看日志文件的内容。例如,对于 cat logfileA.txt
,我得到的只是最后一行乱码:
FINISHED --2019-05-29 17:08:52--me@here:/home/n$ 71913592/3871913592]atmed out). Retrying.
logfileA.txt
的内容是:
--2019-05-29 15:26:50-- http://somesite.com/somepath/a0_FooBar/BarFile.dat
Reusing existing connection to somesite.com:80.
HTTP request sent, awaiting response... 302 Found
Location: http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat [following]
--2019-05-29 15:26:50-- http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat
Resolving cdn.somesite.com (cdn.somesite.com)... xxx.xxx.xx.xx
Connecting to cdn.somesite.com (cdn.somesite.com)|xxx.xxx.xx.xx|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3871913592 (3.6G) [application/octet-stream]
Saving to: 'a0_FooBar/BarFile.dat’
a0_FooBar/BarFile.dat 0%[ ] 0 --.-KB/s
a0_FooBar/BarFile.dat 0%[ ] 15.47K 70.5KB/s
...
a0_FooBar/BarFile.dat 49%[========> ] 1.80G --.-KB/s in 50m 32s
2019-05-29 16:17:23 (622 KB/s) - Read error at byte 1931163840/3871913592 (Connection timed out). Retrying.
--2019-05-29 16:17:24-- (try: 2) http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat
Connecting to cdn.somesite.com (cdn.somesite.com)|xxx.xxx.xx.xx|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3871913592 (3.6G), 1940749752 (1.8G) remaining [application/octet-stream]
Saving to: 'a0_FooBar/BarFile.dat’
a0_FooBar/BarFile.dat 49%[+++++++++ ] 1.80G --.-KB/s
...
a0_FooBar/BarFile.dat 100%[+++++++++==========>] 3.61G 1.09MB/s in 34m 44s
2019-05-29 16:52:09 (909 KB/s) - 'a0_FooBar/BarFile.dat’ saved [3871913592/3871913592]
FINISHED --2019-05-29 17:08:52--
我认为问题可能出在 /
s 或 ---
s 或 >
s 或 ==>
s 或 |
s?
但是由于 wget
的输出可能会有所不同,我如何预测和避免 grep
的任何问题?
命令:
grep -ni --color=never -e "error" -e "fail" logfileA.txt | awk -F: '{print "Line "": "}'
预期输出:
Line 17: 2019-05-29 16:17:23 (622 KB/s) - Read error at byte 1931163840/3871913592 (Connection timed out). Retrying.
此外,ack
线路是否更适合这项工作?如果是这样,what/how?
Wrt I assume the problem could be the /s or ---s or >s or ==>s or |s?
- 不,这些 characters/strings 没有什么特别之处。听起来您可能有 DOS 行结尾 (\r\n
),请参阅 。既然你说 with cat logfileA.txt, all I get is the last line which is garbled
我想知道你是否只有 \r
s 而没有 \n
s 作为行尾。如果你这样做,那么 tr '\r' '\n' < logfileA.txt > tmp && mv tmp logfileA.txt
会解决这个问题。如果那是问题,那么您可以使用 awk -v RS='\r' 'script'
将记录分隔符从默认的 \n
更改为 \r
然后您就不需要这样做 tr
步骤.
虽然你在使用 awk 时不需要 grep。这个:
grep -ni --color=never -e "error" -e "fail" logfileA.txt |
awk -F: '{print "Line "": "}'
可以写成:
awk 'tolower([=11=]) ~ /error|fail/{print "Line "NR":"[=11=]}' logfileA.txt
但是 awk-only 版本更健壮,因为它会正确显示包含 :
的完整行,其中 grep+awk 版本会将它们截断为第一个 :
。
您可以通过将脚本调整为以下方式来处理 DOS 行结尾(如果有):
awk 'tolower([=12=]) ~ /error|fail/{sub(/\r$/,""); print "Line "NR":"[=12=]}' logfileA.txt
并且您可以通过使用 GNU awk 使其查找错误或失败作为独立的单词(而不是其他字符串的一部分,如 terror
或 failles
):
awk -v IGNORECASE=1 -v RS='\r?\n' '/\<(error|fail)\>/{print "Line "NR":"[=13=]}' logfileA.txt
或任何 awk:
awk 'tolower([=14=]) ~ /(^|[^[:alnum:]_])(error|fail)([^[:alnum:]_]|$)/{sub(/\r$/,""); print "Line "NR":"[=14=]}' logfileA.txt
在 bash 文件中,我有 logfileA.txt
包含来自 wget
的输出,我想 运行 grep
检查“错误”或“失败”等词的任何实例,如下所示:
grep -ni --color=never -e "error" -e "fail" logfileA.txt | awk -F: '{print "Line "": "}'
# grep -n line number, -i ignore case; awk to add better format to the line numbers (
但问题是,我认为 logfileA.txt
中的 wget
输出充满了字符,可能会弄乱 grep
的输入,因为我没有得到可靠的匹配.
解决此问题时,我什至无法 cat
可靠地查看日志文件的内容。例如,对于 cat logfileA.txt
,我得到的只是最后一行乱码:
FINISHED --2019-05-29 17:08:52--me@here:/home/n$ 71913592/3871913592]atmed out). Retrying.
logfileA.txt
的内容是:
--2019-05-29 15:26:50-- http://somesite.com/somepath/a0_FooBar/BarFile.dat
Reusing existing connection to somesite.com:80.
HTTP request sent, awaiting response... 302 Found
Location: http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat [following]
--2019-05-29 15:26:50-- http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat
Resolving cdn.somesite.com (cdn.somesite.com)... xxx.xxx.xx.xx
Connecting to cdn.somesite.com (cdn.somesite.com)|xxx.xxx.xx.xx|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3871913592 (3.6G) [application/octet-stream]
Saving to: 'a0_FooBar/BarFile.dat’
a0_FooBar/BarFile.dat 0%[ ] 0 --.-KB/s
a0_FooBar/BarFile.dat 0%[ ] 15.47K 70.5KB/s
...
a0_FooBar/BarFile.dat 49%[========> ] 1.80G --.-KB/s in 50m 32s
2019-05-29 16:17:23 (622 KB/s) - Read error at byte 1931163840/3871913592 (Connection timed out). Retrying.
--2019-05-29 16:17:24-- (try: 2) http://cdn.somesite.com/storage/a0_FooBar/BarFile.dat
Connecting to cdn.somesite.com (cdn.somesite.com)|xxx.xxx.xx.xx|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3871913592 (3.6G), 1940749752 (1.8G) remaining [application/octet-stream]
Saving to: 'a0_FooBar/BarFile.dat’
a0_FooBar/BarFile.dat 49%[+++++++++ ] 1.80G --.-KB/s
...
a0_FooBar/BarFile.dat 100%[+++++++++==========>] 3.61G 1.09MB/s in 34m 44s
2019-05-29 16:52:09 (909 KB/s) - 'a0_FooBar/BarFile.dat’ saved [3871913592/3871913592]
FINISHED --2019-05-29 17:08:52--
我认为问题可能出在 /
s 或 ---
s 或 >
s 或 ==>
s 或 |
s?
但是由于 wget
的输出可能会有所不同,我如何预测和避免 grep
的任何问题?
命令:
grep -ni --color=never -e "error" -e "fail" logfileA.txt | awk -F: '{print "Line "": "}'
预期输出:
Line 17: 2019-05-29 16:17:23 (622 KB/s) - Read error at byte 1931163840/3871913592 (Connection timed out). Retrying.
此外,ack
线路是否更适合这项工作?如果是这样,what/how?
Wrt I assume the problem could be the /s or ---s or >s or ==>s or |s?
- 不,这些 characters/strings 没有什么特别之处。听起来您可能有 DOS 行结尾 (\r\n
),请参阅 with cat logfileA.txt, all I get is the last line which is garbled
我想知道你是否只有 \r
s 而没有 \n
s 作为行尾。如果你这样做,那么 tr '\r' '\n' < logfileA.txt > tmp && mv tmp logfileA.txt
会解决这个问题。如果那是问题,那么您可以使用 awk -v RS='\r' 'script'
将记录分隔符从默认的 \n
更改为 \r
然后您就不需要这样做 tr
步骤.
虽然你在使用 awk 时不需要 grep。这个:
grep -ni --color=never -e "error" -e "fail" logfileA.txt |
awk -F: '{print "Line "": "}'
可以写成:
awk 'tolower([=11=]) ~ /error|fail/{print "Line "NR":"[=11=]}' logfileA.txt
但是 awk-only 版本更健壮,因为它会正确显示包含 :
的完整行,其中 grep+awk 版本会将它们截断为第一个 :
。
您可以通过将脚本调整为以下方式来处理 DOS 行结尾(如果有):
awk 'tolower([=12=]) ~ /error|fail/{sub(/\r$/,""); print "Line "NR":"[=12=]}' logfileA.txt
并且您可以通过使用 GNU awk 使其查找错误或失败作为独立的单词(而不是其他字符串的一部分,如 terror
或 failles
):
awk -v IGNORECASE=1 -v RS='\r?\n' '/\<(error|fail)\>/{print "Line "NR":"[=13=]}' logfileA.txt
或任何 awk:
awk 'tolower([=14=]) ~ /(^|[^[:alnum:]_])(error|fail)([^[:alnum:]_]|$)/{sub(/\r$/,""); print "Line "NR":"[=14=]}' logfileA.txt