bash - 以比 grep-while-prinf 更简单、更优雅的方式从使用 reges 处理的日志文件中提取值
问题描述
我有几个看起来像这样的日志文件:
#LOGa# 180.149.126.169 ## 85 with value 350.00000000000000000000 due brand: 350.00000000000000000000 country: 0 {2020-11-26_11-01-00}
#DETAILS_hits# 180.149.126.169 ## hits=([brand/17]="1" [brand/18]="1" [no_brand]="1" ) {2020-11-26_11-01-00}
#LOG_brand# 180.149.126.169 ## BRANDS=([anyBrand]="1" ) {2020-11-26_11-01-00}
#LOG_country# 180.149.126.169 ## COUNTRY=([anyCountry/17]="1" [anyContinent/18]="1" ) {2020-11-26_11-01-00}
我想提取一些特定日志行的圆顶值
我当然可以去
grep -HiRE "(#LOGa#)(.+)(## )(.+)" --include \myFile.log | while read _ ip _ rank _ value _ _ valueBrand _ _ valueCountry _ ; do printf "%.0f %.0f\n" $valueBrand $valueCountry; done
但不是一种更优雅的方式,比如
cleanME myFile.log "(#LOGa#)($ip)(## )($rank)(with value)($value)(due brand:)($valueBrand)(country:)($valueCountry)(.*)" "$valueBrand.0f $valueCountry.0f"
当然我可以去构建一个这样的函数,但我不记得它比 grep + while + printf 更好的方法
解决方案
如果Perl
碰巧是您的选择,请尝试:
perl -ne '/^#LOGa#\s+([\d.]+)\s+##\s+([\d.]+)\s+with value\s+([\d.]+)\s+due brand:\s+([\d.]+)\s+country:\s+([\d.]+)/ && printf "%.0f %.0f\n", $4, $5' myFile.log
提供的输入的输出:
350 0
- 该选项
-n
告诉Perl
将输入文件逐行处理为sed
. - 该选项
-e
启用单线。 /regex/ && printf ...
只有当该行与正则表达式匹配时,语法才会打印参数grep
。- 正则表达式中的括号创建捕获组,匹配的子字符串可以按顺序用
$1
, , ... 引用。$2
推荐阅读
- javascript - 处理 Fetch API 异常时如何获取详细信息?
- php - 两个if条件和爆炸
- c++ - C++中的字符串向量迭代器
- c++ - 我正在尝试用 C++ 画一个人
- ios - 将数据传递给自定义 UITableViewCell 类
- c# - 无效的标签名称 mimekit
- c# - 记录有通用的基类吗?
- php - 如何将 Laravel Ajax 响应数据绑定到路由链接中
- hibernate - 子对象上 CreateCriteria 时的延迟初始化异常
- google-chrome - 困惑:在 Chrome Dev-Tools Lighthouse 中说要阻止网络选项卡未显示为已阻止的请求