Removing all lines prior to the last pattern in a file/stream


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing all lines prior to the last pattern in a file/stream
# 1  
Old 03-11-2011
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:
Code:
# run_async_command "mark command start test"
# pull_serial_log
^@CLI> [20372:53.558][20372:59.504] command^@ start^@ test^@
command output 1
command output 1
[...]
command output 1
^@CLI> [20372:53.558][20372:59.504] command^@ some^@ other^@ test^@
command output 2
command output 2
[...]
command output 2
^@CLI> [20372:53.558][20372:59.504] command^@ start^@ test^@
command output 3
command output 3
[...]
command output 3
#

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:
Code:
# run_async_command "mark command start test"
# pull_serial_log | sed -n 'MAGIC_GOES_HERE'
^@CLI> [20372:53.558][20372:59.504] command^@ start^@ test^@
command output 3
command output 3
[...]
command output 3
#

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.

Please let me know if anyone wants more details.
# 2  
Old 03-11-2011
Did a quick search on reverse grep, and this should do the trick if you customize it to your problem:

tac /some/log.log | grep -m1 searchword | cut -d ' ' -f7

Seems I'm not allowed to post the link to the article since I'm new here, so you'll have to search for: reverse grep tac, to find it yourself.
# 3  
Old 03-11-2011
My solution would be to scan the output twice -- so that I can identify which pattern is the last one. Something like this:
Code:
lastOccur=`sed -n '/pattern/=' output | tail -1` #will output line numbers matching pattern
sed -n "$lastOccur,$ p" output

---------- Post updated at 11:30 AM ---------- Previous update was at 11:27 AM ----------

Ahhh... 'tac' ... nice one. Then you can do:

Code:
tac output | sed  "/pattern/,$ d" | tac

# 4  
Old 03-11-2011
How about this?
Code:
 awk '/command\^@ start\^@ test\^@/,/^command/{p++;a[p]=$0}{a[p+1]=a[p+1] RS $0}END{print a[p-1] a[p+1]}' file
^@CLI> [20372:53.558][20372:59.504] command^@ start^@ test^@
command output 3
command output 3
[...]
command output 3

# 5  
Old 03-14-2011
Quote:
Originally Posted by mirni
Ahhh... 'tac' ... nice one. Then you can do:
Code:
tac output | sed  "/pattern/,$ d" | tac

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:
Code:
pull_serial_log | get_last "command start test" >> my_output_file

Thanks to all who have responded so far.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

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)
Discussion started by: jxfish2
7 Replies

2. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

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)
Discussion started by: AxeHandle
5 Replies

3. Shell Programming and Scripting

Deleting lines from a stream after matching a pattern

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)
Discussion started by: lorzinian
10 Replies

4. Shell Programming and Scripting

removing lines around a matched pattern

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)
Discussion started by: tamale
9 Replies

5. Shell Programming and Scripting

pattern match in live stream

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)
Discussion started by: phpsnook
3 Replies

6. Shell Programming and Scripting

Delete lines prior to a specific date in a log file.

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)
Discussion started by: ahSher
4 Replies

7. Shell Programming and Scripting

how to get lines prior to the line being searched

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)
Discussion started by: arunrao_oradba
2 Replies

8. UNIX for Dummies Questions & Answers

removing duplicates of a pattern from a file

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)
Discussion started by: ashisharora
3 Replies

9. Shell Programming and Scripting

removing pattern which is spread in multiple lines

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)
Discussion started by: sabyasm
2 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

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)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question