Printing file contents until a pattern is matched

  • Follow


Hi,

I need to print the contents of a file from the beginning until the
given pattern is found, then stop.

Using patterns I need to give both begpat and endpat - is there anyway
to do this giving just the endpat as I would not know what the file
start will be.

Thanks

0
Reply sharmil2006 (1) 11/13/2007 1:19:19 PM

On 13    , 15:19, shrgh <sharmil2...@gmail.com> wrote:
> Hi,
>
> I need to print the contents of a file from the beginning until the
> given pattern is found, then stop.
>
> Using patterns I need to give both begpat and endpat - is there anyway
> to do this giving just the endpat as I would not know what the file
> start will be.
>
> Thanks


/pattern/ { exit }
{ print }

  Vassilis

0
Reply Vassilis 11/13/2007 1:23:03 PM


On Nov 13, 6:23 pm, Vassilis <b.alexand...@gmail.com> wrote:
> On 13    , 15:19, shrgh <sharmil2...@gmail.com> wrote:
>
> > Hi,
>
> > I need to print the contents of a file from the beginning until the
> > given pattern is found, then stop.
>
> > Using patterns I need to give both begpat and endpat - is there anyway
> > to do this giving just the endpat as I would not know what the file
> > start will be.
>
> > Thanks
>
> /pattern/ { exit }
> { print }
>
>   Vassilis

Great! it worked :-) thanks. The pattern is not included,though-any
way to print the pattern and then stop

0
Reply shrgh 11/13/2007 1:35:44 PM


On 11/13/2007 7:35 AM, shrgh wrote:
> On Nov 13, 6:23 pm, Vassilis <b.alexand...@gmail.com> wrote:
> 
>>On 13    , 15:19, shrgh <sharmil2...@gmail.com> wrote:
>>
>>
>>>Hi,
>>
>>>I need to print the contents of a file from the beginning until the
>>>given pattern is found, then stop.
>>
>>>Using patterns I need to give both begpat and endpat - is there anyway
>>>to do this giving just the endpat as I would not know what the file
>>>start will be.
>>
>>>Thanks
>>
>>/pattern/ { exit }
>>{ print }
>>
>>  Vassilis
> 
> 
> Great! it worked :-) thanks. The pattern is not included,though-any
> way to print the pattern and then stop
> 

C'mon, don't tell me you couldn't have figured this one out with a tiny bit of
thoughr:

{ print }
/pattern/ { exit }

	Ed.

0
Reply Ed 11/13/2007 2:14:45 PM

In article <1194959959.390983.115000@d55g2000hsg.googlegroups.com>,
shrgh  <sharmil2006@gmail.com> wrote:
>Hi,
>
>I need to print the contents of a file from the beginning until the
>given pattern is found, then stop.
>
>Using patterns I need to give both begpat and endpat - is there anyway
>to do this giving just the endpat as I would not know what the file
>start will be.

I think the answer you are looking for is:

	NR==1,/endpat/

Interestingly, in my testing, the following did not work, but it seems
like it should.  Anyone have any ideas why?

	1,/endpat/

P.S.  Yes, the first thing that came to my mind was the sort of solution
others have posted, where you exit when you hit the endpat, but a closer
reading of OP's text makes it clear that this is what they are looking
for.

0
Reply gazelle 11/13/2007 2:27:03 PM

On 13 Nov., 15:27, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
> In article <1194959959.390983.115...@d55g2000hsg.googlegroups.com>,
>
> shrgh  <sharmil2...@gmail.com> wrote:
> >Hi,
>
> >I need to print the contents of a file from the beginning until the
> >given pattern is found, then stop.
>
> >Using patterns I need to give both begpat and endpat - is there anyway
> >to do this giving just the endpat as I would not know what the file
> >start will be.
>
> I think the answer you are looking for is:
>
>         NR==1,/endpat/
>
> Interestingly, in my testing, the following did not work, but it seems
> like it should.  Anyone have any ideas why?

Yes. Because the block pattern matching is reset after each block.

Compare it to the code...

    /BEGIN/,/END/

which will print _every_ BEGIN-END section in a file.


>         1,/endpat/

So after /endpat/ has been matched the next block starts where
condition 1 is true, which is always the case, starting from the
subsequent line of /endpat/.

Janis

> P.S.  Yes, the first thing that came to my mind was the sort of solution
> others have posted, where you exit when you hit the endpat, but a closer
> reading of OP's text makes it clear that this is what they are looking
> for.


0
Reply Janis 11/13/2007 2:50:04 PM

In article <1194965404.519982.256910@v2g2000hsf.googlegroups.com>,
Janis  <janis_papanagnou@hotmail.com> wrote:
....
>Yes. Because the block pattern matching is reset after each block.
>
>Compare it to the code...
>
>    /BEGIN/,/END/
>
>which will print _every_ BEGIN-END section in a file.
>
>
>>         1,/endpat/
>
>So after /endpat/ has been matched the next block starts where
>condition 1 is true, which is always the case, starting from the
>subsequent line of /endpat/.

Yes, of course.  Makes perfect sense.

0
Reply gazelle 11/13/2007 3:29:47 PM

6 Replies
236 Views

(page loaded in 0.078 seconds)

Similiar Articles:













7/9/2012 5:40:12 PM


Reply: