Re: for loop (?)
From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 01/29/04
- Next message: Kenny McCormack: "What can and can't be done in shell (Was: Re: system() under ksh)"
- Previous message: Lew Pitcher: "Re: changing my username on unix on MAC"
- In reply to: Icarus Sparry: "Re: for loop (?)"
- Next in thread: Stephane CHAZELAS: "Re: for loop (?)"
- Reply: Stephane CHAZELAS: "Re: for loop (?)"
- Reply: Icarus Sparry: "Re: for loop (?)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 29 Jan 2004 14:49:37 -0600
Icarus Sparry wrote:
<snip>
> awk 'NR>'$line' {exit}
> { f="client" NR "_out" ; print $1 $2 > f ; close(f)}' out
Whenever possible, you should use awk variables in awk scripts instead
of shell variables to avoid problems when those shell variables include
special characters (\, #, newline, etc.), so do this instead:
awk -v line="$line" 'NR>line {exit}
{ f="client" NR "_out" ; print $1 $2 > f ; close(f)}' out
or (if -v not supported by your version of awk):
awk 'NR>line {exit}
{ f="client" NR "_out" ; print $1 $2 > f ; close(f)}' line="$line" out
<snip>
> awk 'BEGIN { for(i=1;i<='$1';++i) { print i ; } exit }'
awk -v max="$1" 'BEGIN { for(i=1;i<=max;++i) print i }'
or
awk '{ for(i=1;i<=max;++i) print i }' max="$1" <<!
!
The latter needs that syntax because assigning variables without -v
means those variables don't apply to the BEGIN section so we have to
move the for loop to the main body and provide some kind of input (I
chose a here document with a blank line).
The specific above cases would probably work fine as originally written,
but it's good to get into the habit of using awk variables so there's no
surprises down the road.
Ed.
- Next message: Kenny McCormack: "What can and can't be done in shell (Was: Re: system() under ksh)"
- Previous message: Lew Pitcher: "Re: changing my username on unix on MAC"
- In reply to: Icarus Sparry: "Re: for loop (?)"
- Next in thread: Stephane CHAZELAS: "Re: for loop (?)"
- Reply: Stephane CHAZELAS: "Re: for loop (?)"
- Reply: Icarus Sparry: "Re: for loop (?)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]