Sponsored Content
Top Forums Shell Programming and Scripting Grep couple of consecutive lines if each lines contains certain string Post 302648609 by black_fender on Wednesday 30th of May 2012 07:01:34 AM
Old 05-30-2012
Grep couple of consecutive lines if each lines contains certain string

Hello,

I want to extract from a file like :
Code:
20120530025502914 | REQUEST | whatever
20120530025502968 | RESPONSE | whatever
20120530025502985 | RESPONSE | whatever
20120530025502996 | REQUEST | whatever
20120530025503013 | REQUEST | whatever
20120530025503045 | RESPONSE | whatever

I want to extract all groups of 2 lines in which the first line contanis 'REQUEST' and the next line below contains 'RESPONSE'

Basically from the above file I would like to have extracted the following :
Code:
20120530025502914 | REQUEST | whatever
20120530025502968 | RESPONSE | whatever
20120530025503013 | REQUEST | whatever
20120530025503045 | RESPONSE | whatever

(Please note those numbers from the first fields to be able to identify which line I've extracted from the initial file - basically the lines in red from the initial file ).
I'm not completely emty-handed, I have this snippet as a starting point ('stolen' a while ago from internet :P ) :
Code:
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=1 s="string" file

Which takes the line I that contains the "string" (in my case "REQUEST" ) and the next line after, but i don;t know where to put the condition that the line after to contain the string "RESPONSE" and if so to extract the respective group of 2 lines.

Last edited by Franklin52; 05-30-2012 at 08:03 AM.. Reason: Please use code tags for data and code samples
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep string & next n lines

need help on this. let say i hv 1 file contains as below: STRING Description bla bla bla Description yada yada yada Data bla bla Data yada yada how do i want to display n lines after the string? thanks in advance! (8 Replies)
Discussion started by: ashterix
8 Replies

2. Shell Programming and Scripting

Grep string but also it will show the next 5 lines

Hi experts, I want to grep a number 9366109380 from a file but it will also show me the next 5 lines. Below is the example- when i grep 989366109380, i can also see the next 5 lines. Line 1. <fullOperation>MAKE:NUMBER:9366109380:PPAY2;</fullOperation> Line 2.... (10 Replies)
Discussion started by: thepurple
10 Replies

3. Shell Programming and Scripting

grep string & a few lines after

i need to grep a STRING_A & the next few lines after the STRING_A example file: STRING_A yada yada line 1 line 2 STRING_B yada yada line 1 line 2 line 3 STRING_A yada yada line 1 line 2 line 3 line 4 STRING_A yada yada line 1 line 2 line 3 line 4 (7 Replies)
Discussion started by: ashterix
7 Replies

4. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

5. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

8. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

9. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

10. Shell Programming and Scripting

Remove duplicate consecutive lines with specific string

Hello, I'm trying to remove the duplicate consecutive lines with specific string "WARNING". File.txt abc; WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 bcd; abc; 123 123 123 WARNING 1234 WARNING 2345 WARNING 2345 efgh; (6 Replies)
Discussion started by: Mannu2525
6 Replies
Net::SIP::Endpoint::Context(3pm)			User Contributed Perl Documentation			  Net::SIP::Endpoint::Context(3pm)

NAME
Net::SIP::Endpoint::Context - Call context for endpoint SYNOPSIS
my $ctx = Net::SIP::Endpoint::Context->new( to => .., from => .. ); my $request = $ctx->new_request(..); DESCRIPTION
This package manages the call context (from,to,call-id, recorded routes..) for a call to an Net::SIP::Endpoint. It maintains the state of the current call (local and remote cseq, current state within INVITE transaction) and handles requests and responses according to this state. CONSTRUCTOR
new ( ARGS ) ARGS is either a hash reference or a hash. It contains the following mandatory keys: from Initiator of call. This is the local address for outgoing calls and the peers address for incoming calls. to Target of call. And the following optional keys: contact Contact info for context. incoming Flag, if the context resulted from an incoming call. auth Authorization info, used if outgoing requests need authorization. See method authorize in Net::SIP::Request for information on the format. route @List of predefined routes (which will be used to create Route SIP header in requests). via @List of predefined Via values used in locally generated responses. This is usually set if the context was created by an incoming request from the Via header in the request. callid Value of Call-Id header. If not given it will be generated. It's usually given for incoming calls, but not for outgoing calls. cseq Initial local sequence number for the CSeq SIP header. Defaults to 0, e.g. the first request will get the sequence number 1. METHODS
callid Returns callid of current call. peer Returns peer of call. For incoming calls this is the sender, for outgoing calls the recipient of the call. new_request ( METHOD, [ BODY, %ARGS ] ) Creates a new requests for method METHOD with body BODY and additional SIP headers defined by %ARGS. The headers for "to", "from", "contact", "route" will be created from the call context info in $self. One special ARGS can be resp40x which should point to a 401/407 SIP response containing the offer from the server for authorization. This way authorization can be fully controlled, e.g. first trying w/o authorization and then retrying with authorization credentials and the 40x response. METHOD might be already a Net::SIP::Request object in which case it will be used unmodified. BODY might be a string or object (see constructor of Net::SIP::Request). It will add the request to the list of active transactions within the context $self, e.g. will be prepared to get responses for it. The callback for the transaction is copied from the default callback for the context, so that it stays the same, even if the default callback changes. It returns the created request object. find_outstanding_requests ( %FILTER ) Returns list of outstanding requests (e.g INVITE w/o reply) for this context. Returns a list of outstanding request (Net::SIP::Request objects) with the most recent requests first. FILTER might be used to restrict the search. With key request a Net::SIP::Request object is expected and it will restrict the search to this object (e.g. it will return the object if it is outstanding). With key method a method can be specified and only requests with this method will be returned. set_callback ( CALLBACK ) Sets callback for context, which will be used if the upper layer need to be notified, e.g on incoming requests or an permanent delivery errors. CALLBACK is a callback usable by invoke_callback in Net::SIP::Util and will be invoked with the following arguments (some arguments make only sense for incoming packets). CTX The call context, e.g. $self. ENDPOINT The Net::SIP::Endpoint object managing CTX. ERROR The errno of the error occured ( undef or 0 if no error ). These are the same numbers defined in Errno, but they are usually not set by a system call, but by the dispatcher (like ETIMEDOUT if delivery failed permanently after none of the retransmits succeeded or EHOSTUNREACH if it cannot resolve the SIP URI). CODE This is the response CODE from an incoming response packet. Undef if the incoming packet was no response. PACKET This is the packet which caused the callback. Only for incoming packets. LEG Net::SIP::Leg where the packet came in. FROM "ip:port" of sender of incoming packet. request_delivery_done ( ENDPOINT, TID, ERROR ) Callback setup at delivery of the packet and called with ERROR FALSE if the packet was delivered successfully over a reliable transport or with ERROR an errno if the packet could not be delivered (or no reply came in for packet, so one can assume that the recipient did not get it). For details on ERROR see set_callback. TID is the transaction ID, see method tid in Net::SIP::Packet. ENDPOINT is the endpoint managing the context $self. handle_response ( RESPONSE,LEG,FROM,ENDPOINT ) Called from the endpoints receive_response method it handles responses to requests originated from the context $self. RESPONSE is the response packet, LEG the leg where the packet came in and FROM the "ip:port" of the sender. ENDPOINT is the endpoint managing the context $self. First it checks if the response matches an active transaction (which begun its life in new_request) and if not it drops the response. Then it checks if the response came in to the right leg, e.g. if the Via header of the packet matches the Via header the leg creates. If not the packet will be dropped. Then it checks if the method in the CSeq header of the response matches the expected method in the transaction (for INVITEs this could be either INVITE or ACK, depending on the state of the transaction). If it not matches the packet will be dropped. (All of these packet drops could be observed if you enable debugging, see Net::SIP::Debug. If the response is for a BYE or CANCEL request the call will be closed. If the response is the unsuccessful final response for an INVITE an ACK will be send, if it's preliminary response it will invoke the callback for the transaction. If the response is a successful final response for an INVITE it will save the routes from the Record-Route header for future requests and create an ACK request. The transaction callback will be invoked and gets as an additional argument the created ACK request, which then can be modified by the callback. The callback should not send the ACK request by itself, it should just modify the given request and sending will be done after the callback returned. If the response is a successful final response to a request other then INVITE it will invoke callback which should fully handle the response. If the response code is 401 (Authorization required) or 407 (Proxy Authorization required) and if the context has authorization info (key auth in the constructor)) it will try to authorize the request based on the realms given in the response and if it can find authorization info for at least parts of the required realms it will redeliver the request. Otherwise it will invoke the callback with an error of EPERM. If the response code is 300 (multiple choice) or 301 (moved permanently) it will invoke the callback because it cannot resolve the issue automatically. But if it's 302 (moved temporally) it will rewrite the request based on the Contact header in the response and redeliver it automatically. If the response is 305 (use proxy) it will take the information from Contact as the upstream proxy and insert it into the routes, so that it will use it as the next hop. Then it rewrites the request for the new routes and redelivers it. For all other responses the callback will be invoked, e.g the issue has to be resolved by the users application. handle_request ( REQUEST,LEG,FROM,ENDPOINT ) Called from the endpoints receive_request method it handles incoming requests for call context $self. REQUEST is the request packet, LEG the leg where the packet came in and FROM the "ip:port" of the sender. ENDPOINT is the endpoint managing the context $self. First it checks if the sequence number of the incoming request (CSeq header) is not lower then the sequence number of the last request received. Otherwise it will drop the request. The it checks if the sequence number is the same as for the last request. If it is higher it must be a new request, otherwise it is a retransmit or an ACK or CANCEL to an INVITE request. If it's a retransmit it will be dropped. If the incoming request is an INVITE it will automatically deliver a response "100 Trying" and then invoke the callback, so that the application might issue "180 Ringing" responses and finally a final response, like "200 Ok". If the incoming request is CANCEL or BYE it will issue a response "200 Closing" and close the context. All other requests must be handled by the application, e.g. the callback will be invoked. perl v5.14.2 2012-04-17 Net::SIP::Endpoint::Context(3pm)
All times are GMT -4. The time now is 09:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy