我有一个具有这种结构的log.text 文件:
user session login_time application database db_connect_time request request_time connection_source connection_ip request_state
+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------
admin 0 9 0 none 0 Not Requested* a00:bf32::
admin 989855740 1335 DRRDEVMH DRRPRODB 1201 none 0 Not Requested a00:8a45::
admin 1768947706 932 test test 916 none 0 Not Requested a00:94b6::
WARNING - 1241024 - Possible string truncation in column 1.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 9.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 10.
WARNING - 1241028 - Output column defined with warnings.
OK/INFO - 1241044 - Records returned: [3].
我们可以在 log.txt 的最后一行看到一个字符串 Records returned: [3]
。数字 3 是我的目标,提取该数字(在本例中为 3)我想在单独的文件中打印下一行。
The total records returned = 3
我正在使用:
sed -n 's@^.*Records returned.*[\(.*\)$@\1@p' log.txt > out.txt
但它没有给出结果。请问我在这里犯了什么错误?
最佳答案
你需要转义[
,试试这个
sed -n 's@^.*Records returned.*\[\(.*\)\].*$@\1@p' log.txt > out.txt
编辑
如果你想打印出这样的字符串
The total records returned = 3
只需在 \1
之前添加 The total records returned =
,因此脚本将是
sed -n 's@^.*Records returned.*\[\(.*\)\].*$@The total records returned = \1@p' log.txt > out.txt
关于linux - 从日志文件中搜索并打印特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20898589/