From: banzai on
Have a file $SCRATCH, print lines 1 to 2:

$ awk 'NR==1,NR==2' ${SCRATCH}
20061104 0000 0000035901
20061104 0005 0000040269

If I want to assign variables to 1 & 2 above it doesn't parse

$ FROM=1
$ TO=2
$ awk 'NR==${FROM},NR==${TO}' $SCRATCH
awk: cmd. line:1: NR==${FROM},NR==${TO}
awk: cmd. line:1: ^ parse error
awk: cmd. line:1: NR==${FROM},NR==${TO}
awk: cmd. line:1: ^ parse error
awk: cmd. line:1: NR==${FROM},NR==${TO}
awk: cmd. line:1: ^ parse error
$

Can this be escaped - Any ideas ?
Thks


From: Barry Margolin on
In article <D1m7h.2672$LK5.1239(a)news.cpqcorp.net>,
"banzai" <banzai(a)aa.com> wrote:

> Have a file $SCRATCH, print lines 1 to 2:
>
> $ awk 'NR==1,NR==2' ${SCRATCH}
> 20061104 0000 0000035901
> 20061104 0005 0000040269
>
> If I want to assign variables to 1 & 2 above it doesn't parse
>
> $ FROM=1
> $ TO=2
> $ awk 'NR==${FROM},NR==${TO}' $SCRATCH
> awk: cmd. line:1: NR==${FROM},NR==${TO}
> awk: cmd. line:1: ^ parse error
> awk: cmd. line:1: NR==${FROM},NR==${TO}
> awk: cmd. line:1: ^ parse error
> awk: cmd. line:1: NR==${FROM},NR==${TO}
> awk: cmd. line:1: ^ parse error
> $
>
> Can this be escaped - Any ideas ?
> Thks

Variables are expanded inside double quotes, but nott inside single
quotes.

awk "$FROM,$TO" $SCRATCH

Although awk seems to be a bit of overkill for this, I'd use

sed -n "${FROM},${TO}p" $SCRATCH

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: banzai on

"Barry Margolin" <barmar(a)alum.mit.edu> wrote in message
news:barmar-35E971.12013017112006(a)comcast.dca.giganews.com...
> In article <D1m7h.2672$LK5.1239(a)news.cpqcorp.net>,
> "banzai" <banzai(a)aa.com> wrote:
>
>> Have a file $SCRATCH, print lines 1 to 2:
>>
>> $ awk 'NR==1,NR==2' ${SCRATCH}
>> 20061104 0000 0000035901
>> 20061104 0005 0000040269
>>
>> If I want to assign variables to 1 & 2 above it doesn't parse
>>
>> $ FROM=1
>> $ TO=2
>> $ awk 'NR==${FROM},NR==${TO}' $SCRATCH
>> awk: cmd. line:1: NR==${FROM},NR==${TO}
>> awk: cmd. line:1: ^ parse error
>> awk: cmd. line:1: NR==${FROM},NR==${TO}
>> awk: cmd. line:1: ^ parse error
>> awk: cmd. line:1: NR==${FROM},NR==${TO}
>> awk: cmd. line:1: ^ parse error
>> $
>>
>> Can this be escaped - Any ideas ?
>> Thks
>
> Variables are expanded inside double quotes, but nott inside single
> quotes.
>
> awk "$FROM,$TO" $SCRATCH
>
> Although awk seems to be a bit of overkill for this, I'd use
>
> sed -n "${FROM},${TO}p" $SCRATCH
>
> --
> Barry Margolin, barmar(a)alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

yep, thanks


From: Ed Morton on
banzai wrote:
> "Barry Margolin" <barmar(a)alum.mit.edu> wrote in message
> news:barmar-35E971.12013017112006(a)comcast.dca.giganews.com...
>
>>In article <D1m7h.2672$LK5.1239(a)news.cpqcorp.net>,
>>"banzai" <banzai(a)aa.com> wrote:
>>
>>
>>>Have a file $SCRATCH, print lines 1 to 2:
>>>
>>>$ awk 'NR==1,NR==2' ${SCRATCH}
>>>20061104 0000 0000035901
>>>20061104 0005 0000040269
>>>
>>>If I want to assign variables to 1 & 2 above it doesn't parse
>>>
>>>$ FROM=1
>>>$ TO=2
>>>$ awk 'NR==${FROM},NR==${TO}' $SCRATCH
>>>awk: cmd. line:1: NR==${FROM},NR==${TO}
>>>awk: cmd. line:1: ^ parse error
>>>awk: cmd. line:1: NR==${FROM},NR==${TO}
>>>awk: cmd. line:1: ^ parse error
>>>awk: cmd. line:1: NR==${FROM},NR==${TO}
>>>awk: cmd. line:1: ^ parse error
>>>$
>>>
>>>Can this be escaped - Any ideas ?
>>>Thks
>>
>>Variables are expanded inside double quotes, but nott inside single
>>quotes.
>>
>>awk "$FROM,$TO" $SCRATCH
>>
<snip>
> yep, thanks
>

To be safe and efficient:

awk -v from="$FROM" -v to="$TO" 'NR>to{exit}NR>=from' "$SCRATCH"

See question 24 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24).

Ed.