Removing all lines prior to the last pattern in a file/stream
Hi all,
I didn't find anything that specifically answers this after searching for a bit, so please forgive me if this has been covered before.
I'm looking to delete all lines prior to the last occurrence of a string in a file or stream from within a shell script (bash.)
A bit of background:
I'm running a command to pull the contents of a circular buffer, so I get whatever happens to be there. I'm only interested in the last string and the contents after that for each command I run. If can operate on the stream as it comes out and before it's written to a file, that'd be ideal. I'd also like to use sed if possible, since I use it in other areas of the script and I want to keep in all in-line.
An pseudo-example of the output I might get is below:
In this case, all I'd want to see is the last instance of "command^@start^@ test^@" followed by its output to the end of the stream/file. Note that the "^@" characters here represent NULL characters. The command output is an arbitrary number of lines - it may be 10 lines, or it may be 1000 lines, and has no specific structure. Here's what I would like to see:
What I plan on doing is running the run_async_command, then dumping the buffer and stripping out only the last command, then run_async_command again with a different command and stripping out only the output from that command, and so on.
My solution would be to scan the output twice -- so that I can identify which pattern is the last one. Something like this: ---------- Post updated at 11:30 AM ---------- Previous update was at 11:27 AM ----------
I think I like this one best so far - I'll fiddle with it in the morning and see if I can get it in a function so I can use it like so:
Thanks to all who have responded so far.
GM,
I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed.
I am assuming that sed, awk or even perl could do what I need.
I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Hi Everybody! First post! Totally noobie.
I'm using the terminal to read a poorly formatted book.
The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this.
I was hoping to use grep -v " -- "... (5 Replies)
Hi,
I have a requirement to to an ldapsearch and remove the shadow attributes in the output file.
What I do is ldapsearch() | operation to remove shadow > FILE
The ldapsearch gives output like this(with same line formation):
objectClass: FSConfig
objectClass: extensibleObject
fsCAIP:... (10 Replies)
I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line.
I've googled for this problem, and most people helping are only interested in... (9 Replies)
hi!
i have a situation like this where i have to analyse the live log generated from
/bin/scp -v you@example.com
is if a pattern like say "Too many connections" comes i shud be able to identify it . (3 Replies)
Hi all.
I have a database log file in which log data get appended to it daily. I want to do a automatic maintainence of this log by going through the log and deleting lines belonging to a certain date.
How should i do it? Please help. Thanks.
Example. To delete all lines prior to Jun... (4 Replies)
Hi,
Can anbody please let me know how i can retrieve lines above the line being searched in a file.
I am looking for an error message from a file, if I see that message I want the lines above that message along with this line.
how do we do this.
Please do let me know
An example which i have... (2 Replies)
hey all,
I need some help.
I have a text file with names in it.
My target is that if a particular pattern exists in that file more than once..then i want to rename all the occurences of that pattern by alternate patterns..
for e.g if i have PATTERN occuring 5 times then i want to... (3 Replies)
I have several huge files wich contains oracle table creation scripts as follows:
I would need to remove the pattern colored in red above. Any sed/awk/pearl code will be of much help.
Thanks (2 Replies)
I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in)
This is on solaris
Can you help? (2 Replies)