LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 04-26-2009, 01:14 AM   #1
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Rep: Reputation: 15
grep lines between occurances of a pattern


Hi,

I have a requirement like, I need to grep all the lines that occur between a pattern.


For e.g.,

Code:
$cat testfile 
123654 asdfasf
46546 asfasd
23456 abcd
54646 need to print
465465 need tp print 
654654 need to print
65465 need to print
23456 abcd
654654 asdfasfuoe
Now i need the lines between the patterns "abcd". Is there a way to achieve this using 'sed' or 'awk'?

Thanks in advance


Sridhar
 
Old 04-26-2009, 01:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# awk '$2=="abcd" && f{f=0;next} $2=="abcd"{ f=1;next}f' file
54646 need to print
465465 need tp print
654654 need to print
65465 need to print
 
Old 04-26-2009, 02:29 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Nice code!

Took me a while to understand as I don't use awk that often
 
Old 04-26-2009, 03:21 AM   #4
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Original Poster
Rep: Reputation: 15
Hi ghostdog74

That was great!! Thanks a ton for the code.


Sridhar
 
Old 04-26-2009, 03:44 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
one thing to take note is i only suggested the code based on the sample file. if there are more sets of abcd, then its a different story.
 
Old 04-26-2009, 06:14 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Here is something that uses sed instead of awk, probably easier to understand but requiring and additional command to filter out the pattern:
Code:
sed -n '/abcd/,/abcd/p' testfile | grep -v abcd
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple pattern search and count the no. of occurances raghu123 Programming 4 06-03-2009 04:50 PM
Replacing a bunch of lines between a pattern Namachivayam Programming 1 05-21-2007 07:23 PM
finding multiple occurances of a pattern sharad Linux - General 3 05-24-2006 03:23 PM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration