Solved

In UNIX grep a phrase and adjacent lines

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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.

770 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