easy for some

Bharat Mediratta bharat at computing-maths.cardiff.ac.uk
Fri May 10 02:12:36 AEST 1991


In article <1991May8.233803.4485 at swsrv1.cirr.com> toma at swsrv1.cirr.com (Tom Armistead) writes:
>In article <6686 at male.EBay.Sun.COM> matthew at gizmo.UK.Sun.COM (Matthew Buller - Sun EHQ - MIS) writes:
>>
>>I am fairly new to unix, and I have a minor question:-
>>problem: to extract text between start and end patterns in a file
>>eg:-
>>
>>file:
>>
>>pattern1---
>>
>>stuff
>>stuff
>>stuff
>>
>>pattern2---
>
>You could do this with sed.
>
>$ sed -n '/^pattern1---$/,/^pattern2---$/p' < data_file
>
>One problem with this is that it prints out the start and end parameters.  You
>may be able to tell SED not to do this, but I don't know how.  So I use egrep.
>
>$ sed -n '/^pattern1---$/,/^pattern2---$/p' < data_file | \
>        egrep -v '^pattern1---$|^pattern2---$'

Well, if the patterns only occur once in the file, here's a simple sed
solution:

	sed -e '1,/^pattern1---$/d' -e '/^pattern2---$/,$d' < data_file

As you can see, it deletes all the stuff up to (and including) the first
pattern, and then all the stuff from the second pattern (inclusive) to
the end of the file.  If you have multiple recurrences of this in the
file, you only get the first one.


--
|  Bharat Mediratta  | JANET: bharat at cm.cf.ac.uk                               |
+--------------------+ UUNET: bharat%cm.cf.ac.uk%cunyvm.cuny.edu at uunet.uucp    |
|On a clear disk...  | uk.co: bharat%cm.cf.ac.uk%cunyvm.cuny.edu%uunet.uucp at ukc|
|you can seek forever| UUCP: ...!uunet!cunym.cuny.edu!cm.cf.ac.uk!bharat       |



More information about the Comp.unix.wizards mailing list