Scripting to Duplicate Lines Based on Variable

Greeting all! I could use some assistance please. :slight_smile:

I've been searching for the best way to duplicate a line based on a variable in the next line.

Sample Data:

Nov 22 00:00:19 10.10.10.1 "%ASA-4-313005: No matching connection for ICMP error message: icmp src Outside:1.2.3.4 dst Inside:69.161.160.162 (type 3, code 2) on Outside interface.  Original IP payload: <unknown>."
Nov 22 00:00:19 10.10.10.1 last message repeated 2 times
Nov 22 00:00:24 10.10.10.1 "%ASA-4-313005: No matching connection for ICMP error message: icmp src Outside:1.2.3.4 dst Inside:69.161.160.162 (type 3, code 2) on Outside interface.  Original IP payload: <unknown>."
Nov 22 00:00:24 10.10.10.1 last message repeated 5 times
Nov 22 00:00:29 10.10.10.1 "%ASA-4-313005: No matching connection for ICMP error message: icmp src Outside:1.2.3.4 dst Inside:69.161.160.162 (type 3, code 2) on Outside interface.  Original IP payload: <unknown>."
Nov 22 00:00:29 10.10.10.1 last message repeated 3 times

I want to duplicate the lines based on how many times the log reports it was was repeated. So in the above example, line 1 would be duplicated 2 times, line 3 would be duplicated 5 times and line 5 would be duplicated 3 times.

I'm at a loss how to accomplish my goal. Anyone have a suggestion?

Thanks

awk ' { line=$0; getline; times=$(NF-1); i=1; while(i<=times) { print line; i++; } } '  logfile
1 Like

This is fantastic! Thank you ever so much!

awk '/last message/{for(i=1; i<$(NF-1); i++) print p; next} {p=$0}1' infile
1 Like