print lines between two patterns

  • Follow


I have a file like

Tuesday
abcdefg
cdfjdkf
kdfkd
dfdfjj
dfkdfjd
Friday
djfkddkf
djfdjdkjf
djfkdj
...
..
Tuesday
...
..
Friday
...
..
..

what I am looking for is to print the lines between first two
partterns "Tuesday -- Friday" and then stop (exit).

the output should like

abcdefg
cdfjdkf
kdfkd
dfdfjj
dfkdfjd

what is simplest way of doing it with awk or sed or perl? 

Thanks in advance.
0
Reply guo 8/7/2003 3:39:56 PM

guo@andrews.edu (Limin Guo) writes:

> I have a file like
> 
> Tuesday
> abcdefg
> cdfjdkf
> kdfkd
> dfdfjj
> dfkdfjd
> Friday
> djfkddkf
> djfdjdkjf
> djfkdj
> ..
> .
> Tuesday
> ..
> .
> Friday
> ..
> .
> .
> 
> what I am looking for is to print the lines between first two
> partterns "Tuesday -- Friday" and then stop (exit).
> 
> the output should like
> 
> abcdefg
> cdfjdkf
> kdfkd
> dfdfjj
> dfkdfjd
> 
> what is simplest way of doing it with awk or sed or perl? 

awk '
$0 == "Friday" && flag {
        exit
}

flag {
        print
}

$0 == "Tuesday" {
        flag = 1
}'

-- 
Best regards, Aleksey Cheusov.
0
Reply Aleksey 8/7/2003 4:22:35 PM



On 8/7/2003 10:39 AM, Limin Guo wrote:
<snip>
> what I am looking for is to print the lines between first two
> partterns "Tuesday -- Friday" and then stop (exit).
> 
> the output should like
> 
> abcdefg
> cdfjdkf
> kdfkd
> dfdfjj
> dfkdfjd
> 
> what is simplest way of doing it with awk or sed or perl? 
> 
> Thanks in advance.

awk '/^Friday$/ && on == 1 {exit}
        on == 1 {print}
        /^Tuesday$/ {on=1}'

Regards,

	Ed.

0
Reply Ed 8/7/2003 7:42:07 PM

In article <3F32AB8F.5040301@Lucent.com>,
Ed Morton  <mortonAVOIDINGSPAM@Lucent.com> wrote:
>
>
>On 8/7/2003 10:39 AM, Limin Guo wrote:
><snip>
>> what I am looking for is to print the lines between first two
>> partterns "Tuesday -- Friday" and then stop (exit).
>> 
>> the output should like
>> 
>> abcdefg
>> cdfjdkf
>> kdfkd
>> dfdfjj
>> dfkdfjd
>> 
>> what is simplest way of doing it with awk or sed or perl? 
>> 
>> Thanks in advance.
>
>awk '/^Friday$/ && on == 1 {exit}
>        on == 1 {print}
>        /^Tuesday$/ {on=1}'

#!gawk
a=/foo/,b=/bar/ {if (a || b) a--;else print}
0
Reply gazelle 8/7/2003 8:08:51 PM

Ed Morton <mortonAVOIDINGSPAM@Lucent.com> wrote in message news:<3F32AB8F.5040301@Lucent.com>...
> On 8/7/2003 10:39 AM, Limin Guo wrote:
> <snip>
> > what I am looking for is to print the lines between first two
> > partterns "Tuesday -- Friday" and then stop (exit).
> > 
> > the output should like
> > 
> > abcdefg
> > cdfjdkf
> > kdfkd
> > dfdfjj
> > dfkdfjd
> > 
> > what is simplest way of doing it with awk or sed or perl? 
> > 
> > Thanks in advance.
> 
> awk '/^Friday$/ && on == 1 {exit}
>         on == 1 {print}
>         /^Tuesday$/ {on=1}'
> 
> Regards,
> 
> 	Ed.


Thank you all for answering my questions. here is another question:
what should I do if two patterns are same? On the other word, the
patterns are all Tuesdays and no Fridays and I need the lines between
from two "Tuesday".
0
Reply guo 8/11/2003 1:14:05 PM


On 8/11/2003 8:14 AM, Limin Guo wrote:
> Ed Morton <mortonAVOIDINGSPAM@Lucent.com> wrote in message news:<3F32AB8F.5040301@Lucent.com>...
<snip>
>>awk '/^Friday$/ && on == 1 {exit}
>>        on == 1 {print}
>>        /^Tuesday$/ {on=1}'
<snip>
> Thank you all for answering my questions. here is another question:
> what should I do if two patterns are same? On the other word, the
> patterns are all Tuesdays and no Fridays and I need the lines between
> from two "Tuesday".

The above solution will still work. Just change "Friday" to "Tuesday".

	Ed.

0
Reply Ed 8/11/2003 2:01:54 PM

5 Replies
1075 Views

(page loaded in 0.133 seconds)

Similiar Articles:













7/23/2012 8:51:51 AM


Reply: