简体   繁体   English

在 awk 中重印动态 RS(记录分隔符)

[英]Reprint a Dynamic RS (Record Separator) in awk

I am using following awk command to print only sections/Record that contain a specific text string (3705@192.168.1.5) in this example.在此示例中,我使用以下 awk 命令仅打印包含特定文本字符串 (3705@192.168.1.5) 的部分/记录。 This works very well but awk removes the record separator while printing out the output.这很有效,但 awk 在打印输出时删除了记录分隔符。 For a generic text based RS, I can use {print RS, $0} to reprint it but in this scenario awk prints regular expression before each record.对于基于通用文本的 RS,我可以使用 {print RS, $0} 重新打印它,但在这种情况下,awk 在每条记录之前打印正则表达式。

awk 'BEGIN{ RS = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]" ;} /3705@192.168.1.5/ {print RS , $0}' /var/log/abc.log awk 'BEGIN{ RS = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[0 -9][0-9][0-9]" ;} /3705@192.168.1.5/ {print RS , $0}' /var/log/abc.log

Is there a way I can print the RS (Timestamp in this case) before each record?有没有办法在每条记录之前打印 RS(在这种情况下为时间戳)? Thanks谢谢

You must already be using gawk since you are using multi-character RS so just print RT instead of RS .您一定已经在使用 gawk 因为您使用的是多字符 RS 所以只需打印RT而不是RS Read the book Effective Awk Programming, 4th Edition, by Arnold Robbins.阅读 Arnold Robbins 所著的 Effective Awk Programming 第 4 版一书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM