在 AWK 中打印上一个字段
我认为 awk 将解决我的问题。我的工具有限,因为我在 ESXi 4.0u1 上使用 busybox。我有一个来自虚拟机备份程序 (ghettoVCB) 的日志文件。我需要扫描此文件中的表达式
“克隆磁盘失败:文件系统上没有足够的空间用于所选操作”
在我的文件中,这位于第 '43' 行左右。前一个字段(在 AWK 词汇中)表示我想要打印
到输出文本文件的虚拟机名称。在我的示例中,虚拟机名称为 TEST12-RH4-AtlassianTest
。
awk 'RS=""
/There is not enough space/ {
print $17
} '
print $17
是硬编码的,我不想要这个。我想找到比上面的正则表达式返回的行上的第一个字段小一个的字段。任何建议表示赞赏。 [Awk 输入文件]
I think awk will be the solution to my problem. My tools are limited b/c I'm using busybox on ESXi 4.0u1. I have a log file from a VM backup program (ghettoVCB). I need to scan this file for the expression
"Failed to clone disk : There is not enough space on the file system for the selected operation"
In my file, this is around line '43'. The previous field (in AWK vocab) represents the VM name that I want to print
to an output text file. In my example the VM name is TEST12-RH4-AtlassianTest
.
awk 'RS=""
/There is not enough space/ {
print $17
} '
print $17
is hard-coded, and I don't want this. I want to find the field that is one less than the first field on the line returned by the regex above. Any suggestions are appreciated.
[Awk Input File]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新(优化版本)
概念验证
更新 2(Uber 优化版本)
从技术上讲,以下内容将是 Uber 优化版本,但它留下了太多错误命中的机会记录分隔符,尽管它适用于您的示例输入。
更新 3(终极超级杀戮优化版本)
我不会在生产代码中使用它,但它只是向您展示总有一种方法可以使其更简单。如果有人能够以代码高尔夫的目的击败这个,我当然希望看到它!
原始
假设您的虚拟机名称始终是您要打印的记录中的最后一个字段,则此方法有效:
Update (Optimized version)
Proof of Concept
Update 2 (Uber optimized version)
Technically, the following would be the uber optimized version but it leaves too much chance for false hits on the record separator, although it works for your sample input.
Update 3 (Ultimate mega-kill optimized version)
I wouldn't use this in production code, but it just goes to show you there is always a way to make it simpler. If somebody can beat this for code golf purposes, I'd certainly like to see it!
Original
Assuming your VM name is always the last field in the record you want to print, this works:
所以你不能使用类似的东西吗
So couldn't you use something like