Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

In UNIX grep a phrase and adjacent lines

Posted on 2010-11-10
2
Medium Priority
?
959 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
Zakabog earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

722 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