I have two values “usb 1-3” and “address 20”. Now I am trying to search a text file ( basically a log file) which has both of these words.
Now that file has many lines with these words as:
Apr 27 13:30:55 box2 kernel: usb 1-3: USB disconnect, address 20
and
Apr 27 13:25:43 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 20
Now my requirement is that: I want to get the line with the second-to-last occurrence of these two words and then next 16 lines after that. Like if there are 6 occurrences in file (6 lines with both of these words) then I need the line with the 5th occurrence and the next 16 lines after it. If there are 10 occurrences then I need the 9th occurrence and the next 16 lines.
currently I was using
egrep -A 20 'usb 1-3:.*address 20' filename | tail -16 > output.
but it gives me last occurrence. not second-to-last. please note that line should have Both Words and in same order (usb 1-3 first and address 20 later)
Please tell me if my question isn’t clear. Thanks in advance.
This requires two passes, but it should do the trick:
You could use
awkorperlto do it in one pass, but you’d need to manage some buffers. Note that it usestac, which is a reversecat. If you don’t have it on your system, try to droptail -rin its place.