LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-15-2009, 04:53 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
grep till the 1st occurrence of a pattern


Hi I have a file like this

@<TRIPOS>MOLECULE
drugbank
1 1 -3.354658 1.791581 -0.875416 P.3 1 UNNAMED1 1.512600
2 2 -1.966044 0.997785 -0.876839 O.3 1 UNNAMED1 -0.551200
3 3 3.304945 1.379568 -0.783382 O.3 1 UNNAMED1 -0.532500
@<TRIPOS>SUBSTRUCTURE
1 UNNAMED1 1
@<TRIPOS>MOLECULE
drugbank_1
@<TRIPOS>ATOM
1 1 -3.774815 1.404774 -0.326354 P.3 1 UNNAMED1 1.512600
2 2 -2.271365 1.098882 0.128466 O.3 1 UNNAMED1 -0.551200
3 3 3.716964 0.714537 -1.002311 O.3 1 UNNAMED1 -0.532500
4 4
.....................................
so on

I want to grep the lines between "@<TRIPOS>MOLECULE" and "@<TRIPOS>SUBSTRUCTURE"...........THere are many occurances of the 2 patterns that i want.........
I am using sed -n '/@<TRIPOS>MOLECULE/,/@<TRIPOS>SUBSTRUCTURE/p' file
and
awk '/@<TRIPOS>MOLECULE/,/@<TRIPOS>SUBSTRUCTURE/' file

But they are giving all the occurances.........I want only the first occurance between the patterns........I do not want to use "csplit" command and then do the job which is very indirect......please suggest
 
Old 04-15-2009, 05:09 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,012

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
Code:
awk '/@<TRIPOS>MOLECULE/{to_print=1}
     /@<TRIPOS>SUBSTRUCTURE/{print; nextfile}
     to_print == 1' file
 
Old 04-15-2009, 05:47 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Code:
perl -ne 'print if /@<TRIPOS>MOLECULE/.../@<TRIPOS>SUBSTRUCTURE/ && exit' file
awk
Code:
# awk '/@<TRIPOS>SUBSTRUCTURE/{print;exit}/@<TRIPOS>MOLECULE/{f=1}f' file
@<TRIPOS>MOLECULE
drugbank
1 1 -3.354658 1.791581 -0.875416 P.3 1 UNNAMED1 1.512600
2 2 -1.966044 0.997785 -0.876839 O.3 1 UNNAMED1 -0.551200
3 3 3.304945 1.379568 -0.783382 O.3 1 UNNAMED1 -0.532500
@<TRIPOS>SUBSTRUCTURE

Last edited by ghostdog74; 04-15-2009 at 05:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep till the 1st occurrence of a pattern raghu123 Programming 1 04-15-2009 05:17 AM
Need ideas on how to grep for pattern using awk rahulbgl Linux - Newbie 4 03-05-2007 03:07 PM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 03:43 PM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration