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
# 8  
Old 02-24-2014
Hello,

One more approach for same.


Code:
awk -vs1="Server" 'BEGIN{ print s1 "Stopped" } /^[0-9]/ {print} END{print s2 "Started"}' starting_ending_text

Output will be as follows.


Code:
Server Stopped
123
456
789
Server Started



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 02-24-2014
Try something like :
Code:
$ tail -$(grep -m1 -n 'Server Stopped' <(tac file) | cut -d':' -f1) file
Server Stopped
123
456
789
Server Started

This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 02-24-2014
python shell:
Code:
>>> import re
>>> text = '''
... 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
... '''
>>> pattern = '(^Server Stopped\s*\n^(?:\d+\n)+^Server Started)'
>>> for found in re.search(pattern,text,re.M).groups():                                
...    print found
... 
Server Stopped
123
456
789
Server Started

This User Gave Thanks to Klashxx For This Post:
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