Solved

In UNIX grep a phrase and adjacent lines

Posted on 2010-11-10
2
944 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 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

622 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