Solved

In UNIX grep a phrase and adjacent lines

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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 …
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

710 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