We have very large transaction logs that have transactions which start with a line that starts with 'Begin :' and ends with a line that starts with 'End :'. For most transactions there is valid data between those two lines.
I am trying to get rid of lines that look like this:
Begin : Transaction Id 1245 04-Oct-2011 04:01:48 Username smith
End : Transaction Id 1245 04-Oct-2011 04:01:49
So, get rid of the first line, then get rid of the very next line if that next line starts with 'End :'. If there is data between, I want to preserve those lines throughout the entire file.
I cut and pasted but it doesn't look like there are the right amount of spaces in my post - there should be three spaces after 'Begin' and before the ':'. With 'End', there are five spaces before the ':'.
Let me try pasting again.
Begin : Transaction Id 1234 04-Oct-2011 13:44:22.24 Username smith
End : Transaction Id 1234 04-Oct-2011 13:44:22.25
I modified and ran again: nawk '/^End && A { A=""; next } A { print A ; A="" } /^Begin { A=$0 ; next } 1' 20111005.trl > testnawk &
I think this may have solved it but need to do some testing on a much bigger file.
Is this finding 'End :' putting that in a place holder, then finding if 'Begin :' is the next line above and deleting both or printing everything but those lines ?
Yes it's storing the line in A and printing later, have a look a ahamed101's solution it just calls getline to throw away the being line and process the line following.
If you only want to retain the Begin/End block who contains at least 1 or more lines (and keeping the Begin and End lines of those blocks) then you can go for :