GREP between last occurrences of two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting GREP between last occurrences of two strings
# 1  
Old 02-24-2014
GREP between last occurrences of two strings

Hi I have the server.log file as:

Code:
Server Stopped
ABC
DEF
GHI
JKL
Server Started
MNO
PQR
STU
Server Stopped
VWX
YZ
ABC
Server Started
Server Stopped
123
456
789
Server Started



I want to get only this in the output to a log file :

Server Stopped
123
456
789
Server Started

i.e. The text between last Server stopped and Server Started and ignore others.

Can someone please help. I had look into tac but not getting the exact solution.
# 2  
Old 02-24-2014
An awk approach:
Code:
awk '
        /Server Stopped/ {
                f = 1
                S = $0
                next
        }
        f && !/Server Started/ {
                S = S RS $0
                next
        }
        /Server Started/ {
                f = 0
                S = S RS $0
        }
        END {
                print S
        }
' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-24-2014
Hi,
Another solution with sed:
Code:
sed -n '/Server Stopped/h;/Server Stopped/,/Server Started/{/Server Stopped/b;H;};${x;p;}' file

Regards.
This User Gave Thanks to disedorgue For This Post:
# 4  
Old 02-24-2014
Hi

Can you please explain the sed one...I am confused with the last part
# 5  
Old 02-24-2014
/Server Stopped/h ==> if line we found 'Server Stopped', we initialize hold buffer with it.
/Server Stopped/,/Server Started/{/Server Stopped/b,H;} ==> Between lines 'Server Stopped' and 'Server Started', if the line is 'Server Stopped', we will at the end of the sed command to begin to the next line, else adds the other lines in the hold buffer.
${x,p;} ==> if last line, swap the hold buffer and the current buffer and the current buffer is displayed.
This User Gave Thanks to disedorgue For This Post:
# 6  
Old 02-24-2014
Try also (untested):
Code:
tac server.log | awk '/server started/,/server stopped/; /server stopped/ {exit}' | tac

# 7  
Old 02-24-2014
Just another awk version

Code:
awk '/Server Stopped/{f=""}/Server Stopped/,/Server Started/{f=f RS $0}END{print f}' infile

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line),... (2 Replies)
Discussion started by: samjna
2 Replies

2. Shell Programming and Scripting

Grep strings for different cases

Hi All, Good morning I have a below code which is working & getting expected output. the problem in this code is it is executing 3 if conditions, my requirement is suppose if first condition is success then it should print echo statement & exit from if condition else if the 1st if condition... (4 Replies)
Discussion started by: sam@sam
4 Replies

3. Shell Programming and Scripting

Grep for strings

Hi, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 How can I grep for the strings chrome and safari from a file, and if chrome print Chrome/40.0.2214.94 to a file and also count the number of times chrome is found? ... (4 Replies)
Discussion started by: cyberfrog
4 Replies

4. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

5. Homework & Coursework Questions

Du without directory and Grep for occurrences of a word

Assistance on work Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Files stored in ... (1 Reply)
Discussion started by: alindner
1 Replies

6. Shell Programming and Scripting

PERL : Sort substring occurrences in array of strings

Hi, My developer is on vacation and I am not sure if there is something which is easier for this. I have an array of strings. Each string in the array has "%" characters in it. I have to get the string(s) which have the least number of "%" in them. I know how I can get occurrences : ... (7 Replies)
Discussion started by: sinpeak
7 Replies

7. UNIX for Dummies Questions & Answers

Replace all occurrences of strings with parentheses

Hi, I tried to adapt bartus's solution to my problem, without success. I want to replace all the occurences of this: with: , where something can contain an arbitrary number of balanced parens and brakets. Any ideas ? Best, (1 Reply)
Discussion started by: ff1969ff1969
1 Replies

8. Shell Programming and Scripting

how many occurrences of different strings are there in each FILE.

Hello , I need some help to pull the data from different files, simultaneously for the string provided. I want to search below strings. PTN:3763427632478 IDB:3298734287438 PTN:8734983298738 From the files BELOW CODE_FILE_LOG1 CODE_FILE_LOG2 CODE_FILE_LOG3 CODE_FILE_LOG4 (3 Replies)
Discussion started by: baraghun
3 Replies

9. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

10. UNIX for Dummies Questions & Answers

grep unique occurrences

Hi, How do i grep unique occurrences from a file. I have a log file with multiple occurrences of 'foo' for instance. When i do: grep foo, I get all the lines that contain foo. Is there any way to get only one line for foo? Example file: foo at 09.01am Fri 11 May 2007 foo at 09.13am... (6 Replies)
Discussion started by: mz043
6 Replies
Login or Register to Ask a Question