Imran Asif
Imran Asif

Reputation: 21

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 ([email protected]) in this example. This works very well but awk removes the record separator while printing out the output. 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.

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

Is there a way I can print the RS (Timestamp in this case) before each record? Thanks

Upvotes: 1

Views: 247

Answers (1)

Ed Morton
Ed Morton

Reputation: 204164

You must already be using gawk since you are using multi-character RS so just print RT instead of RS. Read the book Effective Awk Programming, 4th Edition, by Arnold Robbins.

Upvotes: 1

Related Questions