Sponsored Content
Top Forums Shell Programming and Scripting Grep three consecutive lines if each lines contains certain string Post 302960748 by RavinderSingh13 on Thursday 19th of November 2015 07:14:13 AM
Old 11-19-2015
Hello Saumitra,

If understood your requirement correctly then following may help you in same where you need to have columns values in sequence of REQUEST RESPONSE SUCCESS and column 1 should have all 3 then only it should print it. If you have some other requirement please let us know with sample input and all conditions with sample expected output.
Code:
awk -F' +| +' 'FNR==NR{A[$1 OFS $3]=$0;next} (($1 OFS "REQUEST") in A){O=A[$1 OFS "REQUEST"];B[$1]++} (($1 OFS "RESPONSE") in A){O=O ORS A[$1 OFS "RESPONSE"];;B[$1]++} (($1 OFS "SUCCESS") in A){O=O ORS A[$1 OFS "SUCCESS"];;B[$1]++}{if(B[$1]==3 && O !~ /^$/){print O;O="";}}'  Input_file Input_file

Output will be as follows.
Code:
2914 | REQUEST | whatever
2914 | RESPONSE | whatever
2914 | SUCCESS | whatever
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever

EDIT: Adding a non-one liner form for solution here too.
Code:
awk -F' +| +' 'FNR==NR{
                        A[$1 OFS $3]=$0;
                        next
                      }
               (($1 OFS "REQUEST") in A)        {
                                                        O=A[$1 OFS "REQUEST"];
                                                        B[$1]++
                                                }
               (($1 OFS "RESPONSE") in A)       {
                                                        O=O ORS A[$1 OFS "RESPONSE"];
                                                        B[$1]++
                                                }
               (($1 OFS "SUCCESS") in A)        {
                                                        O=O ORS A[$1 OFS "SUCCESS"];
                                                        B[$1]++
                                                }
                                                {
                                                        if(B[$1]==3 && O !~ /^$/){
                                                                                        print O;
                                                                                        O="";
                                                                                        i=""
                                                                                 }
                                                }
               ' Input_file  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-19-2015 at 08:23 AM.. Reason: Added a non-one liner form of solution now.
 

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

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

aix :grep to get lines before and after string

am using AIX and I have a string "There is no process to read data written to a pipe". I want to get the output 2 lines before and 4 lines after this string. The string is present like more than 100 times in the log and I want to output, the last result in the log with this string I tried using... (1 Reply)
Discussion started by: PhAnT0M
1 Replies

7. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

8. 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

9. 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

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
IO::Async::Sequencer(3pm)				User Contributed Perl Documentation				 IO::Async::Sequencer(3pm)

NAME
"IO::Async::Sequencer" - handle a serial pipeline of requests / responses (EXPERIMENTAL) SYNOPSIS
When used as a client: use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $sock = ... my $sequencer = IO::Async::Sequencer->new( handle => $sock, on_read => sub { my ( $self, $buffref, $eof ) = @_; return 0 unless $$buffref =~ s/^(.*) //; my $line = $1; $line =~ m/^RESPONSE (.*)$/ and $self->incoming_response( $1 ), return 1; print STDERR "An error - didn't recognise the line $line "; }, marshall_request => sub { my ( $self, $request ) = @_; return "REQUEST $request "; }, ); $loop->add( $sequencer ); $sequencer->request( request => "hello", on_response => sub { my ( $response ) = @_; print "The response is: $response "; }, ); When used as a server: my $sequencer = IO::Async::Sequencer->new( handle => $sock, on_read => sub { my ( $self, $buffref, $eof ) = @_; return 0 unless $$buffref =~ s/^(.*) //; my $line = $1; $line =~ m/^REQUEST (.*)$/ and $self->incoming_request( $1 ), return 1; print STDERR "An error - didn't recognise the line $line "; }, on_request => sub { my ( $self, $token, $request ) = @_; # Now to invoke the application logic, whatever it may be solve_request( request => $request, on_completed => sub { my ( $response ) = @_; $self->respond( $token, $response ); } ); }, marshall_response => sub { my ( $self, $response ) = @_; return "RESPONSE $response "; }, ); DESCRIPTION
This module provides an EXPERIMENTAL subclass of "IO::Async::Stream" which may be helpful in implementing serial pipeline-based network protocols of requests and responses. It deals with low-level details such as pairing up responses to requests in ordered protocols, and allows a convenient location to store the line stream marshalling and demarshalling code. The intention is that individual network protocols may be implemented as a subclass of this class, providing the marshalling and demarshalling code itself, providing a clean implementation to be used by the using code. An example protocol that would be easy to implement in this way would be HTTP. Objects in this class can operate in any of three ways: o A pure client The object is asked to send requests by the containing code and invokes response handling code when responses arrive. o A pure server The object receives requests from the filehandle, processes them, and sends responses back, but does not initiate any traffic of its own. o Mixed The object behaves as a mixture of the two; initiating requests, as well as responding to those of its peer connection. The exact mode of operation of any object is not declared explicitly, but instead is an artefact of the set of callbacks provided to the constructor or methods defined by the subclass. Certain callbacks or methods only make sense for one mode or the other. The various operations required can each be provided as callback functions given in keys to the constructor, or as object methods on a subclass of this class. Keys passed to the constructor will take precidence over defined methods. As it is still EXPERIMENTAL, any details of this class are liable to change in future versions. It shouldn't yet be relied upon as a stable interface. PARAMETERS
The following named parameters may be passed to "new" or "configure": on_read => CODE As for IO::Async::Stream. The code here should invoke the "incoming_request" or "incoming_response" methods when appropriate, after having parsed the incoming stream. See the SYNOPSIS or EXAMPLES sections for more detail. Each request can optionally provide its own handler for reading its response, using the "on_read" key to the "request" method. The handler provided to the constructor is only used if this is not provided. on_request => CODE A callback that is invoked when the "incoming_request" method is called (i.e. when operating in server mode). It is passed the request, and a token to identify it when sending a response. $on_request->( $self, $token, $request ); The token should be considered as an opaque value - passed into the "respond" method when a response is ready, but not otherwise used or modified. marshall_request => CODE marshall_response => CODE Callbacks that the "request" or "respond" methods will use, respectively, to stream a request or response object into a string of bytes to write to the underlying file handle. $string = $marshall_request->( $self, $request ); $string = $marshall_response->( $self, $response ); These are used respectively by the client and server modes. pipeline => BOOL Optional. Controls whether requests will be pipelined; that is, all requests will be sent by the client before responses are received. If this option is disabled, only the first request will be sent. Other requests will be queued internally, and each will be sent when the response to the previous has been received. Defaults enabled; supply a defined but false value to disable. SUBCLASS METHODS
This class is intended as a base class for building specific protocol handling code on top of. These methods are intended to be called by the specific subclass, rather than the containing application code. $sequencer->incoming_request( $request ) To be called from "on_read". This method informs the sequencer that a new request has arrived. It will invoke "on_request", passing in a token to identify the request for stream ordering purposes, and the request itself. $sequencer->incoming_response( $response ) To be called from "on_read". This method informs the sequencer that a response has arrived. It will invoke "on_response" that had been passed to the "request" method that sent the original request. PUBLIC METHODS
These methods are intended to be called by the application code using a subclass of this class. $sequencer->request( %params ) Called in client mode, this method sends a request upstream, and awaits a response to it. Can be called in one of two ways; either giving a specific "on_read" handler to be used when the response to this request is expected, or by providing an "on_response" handler for when the default handler invokes "incoming_response". The %params hash takes the following arguments: request => SCALAR The request value to pass to "marshall_request". on_response => CODE A continuation to invoke when a response to this request arrives from the upstream server. It will be invoked as $on_response->( $response ); on_read => CODE A callback to use to parse the incoming stream while the response to this particular request is expected. It will be invoked the same as for "IO::Async::Stream"; i.e. $on_read->( $self, $buffref, $eof ) This handler should return "undef" when it has finished handling the response, so that the next one queued can be invoked (or the default if none exists). It MUST NOT call "incoming_response". Instead, the code should directly implement the behaviour for receipt of a response. If the "on_read" key is used, it is intended that a specific subclass that implements a specific protocol would construct the callback code in a method it provides, intended for the using code to call. $sequencer->respond( $token, $response ) Called in server mode, usually at the end of "on_request", or some continuation created within it, this method sends a response back downstream to a client that had earlier requested it. $token The token that was passed into the "on_request". Used to ensure responses are sent in the right order. $response The response value to pass to "marshall_response". EXAMPLES
A simple line-based server The following sequencer implements a simple server which takes and responds with CRLF-delimited lines. package LineSequencer; use base qw( IO::Async::Sequencer ); my $CRLF = "x0dx0a"; # More portable than sub on_read { my ( $self, $buffref, $eof ) = @_; while( $buffref =~ s/^(.*)$CRLF// ) { $self->incoming_request( $1 ); } return 0; } sub marshall_response { my ( $self, $response ) = @_; return $response . $CRLF; } 1; The server could then be used, for example, as a simple echo server that replies whatever the client said, in uppercase. This would be done using an "on_request" like the following. my $linesequencer = LineSequencer->new( handle => ... on_request => sub { my ( $self, $token, $request ) = @_; $self->respond( $token, uc $request ); } ); It is likely, however, that any real use of the server in a non-trivial way would perform much more work than this, and only call "$self->respond" in an eventual continuation at the end of performing its work. The $token is used to identify the request that the response responds to, so that it can be sent in the correct order. Per-request "on_read" handler If an "on_read" handler is provided to the "request" method in client mode, then that handler will be used when the response to that request is expected to arrive. This will be used instead of the "incoming_response" method and the "on_response" handler. If every request provides its own handler, then the one in the constructor would only be used for unrequested input from the server - perhaps to generate an error condition of some kind. my $sequencer = IO::Async::Sequencer->new( ... on_read => sub { my ( $self, $buffref, $eof ) = @_; print STDERR "Spurious input: $$buffref "; $self->close; return 0; }, marshall_request => sub { my ( $self, $request ) = @_; return "GET $request" . $CRLF; }, ); $sequencer->request( request => "some key", on_read => sub { my ( $self, $buffref, $eof ) = @_; return 0 unless $$buffref =~ s/^(.*)$CRLF//; my $line = $1; print STDERR "Got response: $1 " if $line =~ m/^HAVE (.*)$/; return undef; # To indicate that this response is finished } ); TODO
o Some consideration of streaming errors. How does the "on_read" signal to the containing object that a stream error has occured? Is it fatal? Can resynchronisation be attempted later? o Support, either here or in a different class, for out-of-order protocols, such as IMAP, where responses can arrive in a different order than the requests were sent. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Sequencer(3pm)
All times are GMT -4. The time now is 08:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy