From: moonhkt on
Hi All

How to grep multi-line, when input particlar date ?

Text file
2010/06/21 Canon Scanner 9000F
2010/06/30 MacBook Pro Delivery, Original 2010/07/02
S/N
etc...
2010/07/06 Mail Rebate Form to MAC Apple, http://www.apple.com/hk/promo/rebate/bts.html

input 2010/06/30 return following line ? Under date string may be
more than one line. This example return 3 lines.
2010/06/30 MacBook Pro Delivery, Original 2010/07/02
S/N
etc...

moonhkt
From: Janis Papanagnou on
moonhkt schrieb:
> Hi All
>
> How to grep multi-line, when input particlar date ?
>
> Text file
> 2010/06/21 Canon Scanner 9000F
> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
> S/N
> etc...
> 2010/07/06 Mail Rebate Form to MAC Apple, http://www.apple.com/hk/promo/rebate/bts.html
>
> input 2010/06/30 return following line ? Under date string may be
> more than one line. This example return 3 lines.
> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
> S/N
> etc...
>
> moonhkt

One approach is

awk -v d="$1" '(!/^ / && f=($1~d)) || (f && /^ /)'


Janis
From: Ed Morton on
On 7/13/2010 1:32 AM, moonhkt wrote:
> Hi All
>
> How to grep multi-line, when input particlar date ?
>
> Text file
> 2010/06/21 Canon Scanner 9000F
> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
> S/N
> etc...
> 2010/07/06 Mail Rebate Form to MAC Apple, http://www.apple.com/hk/promo/rebate/bts.html
>
> input 2010/06/30 return following line ? Under date string may be
> more than one line. This example return 3 lines.
> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
> S/N
> etc...
>
> moonhkt

Try this (untested):

awk -v d="2010/07/06" '$0 ~ /^[[:digit:]]/ {f = ($1 == d)} f' file

Ed.
From: Ben Bacarisse on
Ed Morton <mortonspam(a)gmail.com> writes:

> On 7/13/2010 1:32 AM, moonhkt wrote:
>> How to grep multi-line, when input particlar date ?
>>
>> Text file
>> 2010/06/21 Canon Scanner 9000F
>> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
>> S/N
>> etc...
>> 2010/07/06 Mail Rebate Form to MAC Apple, http://www.apple.com/hk/promo/rebate/bts.html
<snip>
> Try this (untested):
>
> awk -v d="2010/07/06" '$0 ~ /^[[:digit:]]/ {f = ($1 == d)} f' file

Is there a reason to prefer "$0 ~ /^[[:digit:]]/" over the simpler
"/^[[:digit:]]/" as the pattern?

--
Ben.
From: Ed Morton on
On 7/13/2010 8:13 AM, Ben Bacarisse wrote:
> Ed Morton<mortonspam(a)gmail.com> writes:
>
>> On 7/13/2010 1:32 AM, moonhkt wrote:
>>> How to grep multi-line, when input particlar date ?
>>>
>>> Text file
>>> 2010/06/21 Canon Scanner 9000F
>>> 2010/06/30 MacBook Pro Delivery, Original 2010/07/02
>>> S/N
>>> etc...
>>> 2010/07/06 Mail Rebate Form to MAC Apple, http://www.apple.com/hk/promo/rebate/bts.html
> <snip>
>> Try this (untested):
>>
>> awk -v d="2010/07/06" '$0 ~ /^[[:digit:]]/ {f = ($1 == d)} f' file
>
> Is there a reason to prefer "$0 ~ /^[[:digit:]]/" over the simpler
> "/^[[:digit:]]/" as the pattern?
>

No, that's just what happens when you start off thinking $1 ~ ... then realise
$0 is at least as appropriate.

Ed.