If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > print lines between two patterns in unix

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-11-10, 23:27
jhonnyrip jhonnyrip is offline
Registered User
 
Join Date: Sep 2008
Posts: 4
print lines between two patterns in unix

sample :

Detroit
Chicago
Newyork
Battlecreek
Jackson
Brooklyn


How would I print only lines match between Detroit and Brooklyn ?
I don't want print Detroit and Brooklyn

output should be :
Chicago
Newyork
Battlecreek
Jackson

Thanks

Jhonny
Reply With Quote
  #2 (permalink)  
Old 08-12-10, 07:25
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
#!/bin/ksh
print=no
while read line
do
if [ $line = Brooklyn ]
then
print=no
fi
if [ $print = yes ]
then
echo $line
fi
if [ $line = Detroit ]
then
print=yes
fi
done
Reply With Quote
  #3 (permalink)  
Old 08-12-10, 09:37
jhonnyrip jhonnyrip is offline
Registered User
 
Join Date: Sep 2008
Posts: 4
thanks for reply.

can it be done using sed or awk command ?
Reply With Quote
  #4 (permalink)  
Old 08-12-10, 10:07
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,612
Quote:
Originally Posted by NZDF
#!/bin/awk
{if (/Chicago/) {p=1}; if (/Brooklin/) {p=0}; if (p) {print}}
Note that this code works fine, but would likely get you failed if you turn it in as a classroom assignment.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #5 (permalink)  
Old 08-12-10, 11:30
jhonnyrip jhonnyrip is offline
Registered User
 
Join Date: Sep 2008
Posts: 4
did not work
awk {if (/Chicago/) {p=1}; if (/Brooklin/) {p=0}; if (p) {print}} q1
-bash: syntax error near unexpected token `('
thanks
Reply With Quote
  #6 (permalink)  
Old 08-12-10, 12:27
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Why do you suppose you are getting a bash error in an awk script.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On