Solved

In UNIX grep a phrase and adjacent lines

Posted on 2010-11-10
2
913 Views
Last Modified: 2012-05-10
I like to cat the file and grep a phrase and necxt 3 lines

EX
<>
 SELECT t1.commerce_item_id,t1.type,t3.ship_date
   FROM dcspp_item t1
   LEFT OUTER JOIN usps_label_items t3 ON t3.commerce_item_id=t1.commerce_item

  WHERE t2.order_id=t1.order_ref
    AND ((t1.type = ?)
    AND ((t2.profile_id = ?)
    AND ((t2.state = ?)
    OR (t2.state = ?)
    OR (t2.state = ?)
    OR (t2.state = ?)
    OR (t2.state = ?))
    AND (t3.ship_date >= ?)
    AND (t3.ship_date <= ?)))
 ORDER BY t3.ship_date ASC
-- Parameters --
p[1] = {pd: type} 4003 (java.lang.Integer)
p[2] = {pd: profileId} 1320006 (java.lang.String)
p[3] = {pd: state} SUBMITTED (java.lang.String)
p[4] = {pd: state} PROCESSING (java.lang.String)
p[5] = {pd: state} PENDING_MERCHANT_ACTION (java.lang.String)
p[6] = {pd: state} PENDING_CUSTOMER_ACTION (java.lang.String)
p[7] = {pd: state} NO_PENDING_ACTION (java.lang.String)
p[8] = {pd: shipDate} 2010-11-09 (java.sql.Date)
p[9] = {pd: shipDate} 2010-11-14 (java.sql.Date)
<>

When I grep SELECT, for above lines of code I like to see only 3 lines

SELECT t1.commerce_item_id,t1.type,t3.ship_date
   FROM dcspp_item t1
   LEFT OUTER JOIN usps_label_items t3 ON t3.commerce_item_id=t1.commerce_item

There are several SELECT ( 300) in the 5000 lines of file,
I like to grep each SELECT and next 2 lines


Is this possible to do

If so, I'll appreciate, if any one can help

Thanks
0
Comment
Question by:Greens8301
2 Comments
 
LVL 3

Accepted Solution

by:
Zakabog earned 500 total points
ID: 34105615
grep -A2 SELECT

That will return the line matching and the next two lines, found the answer here -

http://linux.byexamples.com/archives/304/grep-multiple-lines/

You can also do

grep -A2 -i select

so it matches upper or lower case (-i is ignore case)
0
 

Author Closing Comment

by:Greens8301
ID: 34105878
Quick and Very excellent solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now