Prev: building the printf format for a variable number of args
Next: changing the second + in a bash script
From: Janis Papanagnou on 30 Mar 2010 13:41 Ed Morton wrote: >>[...] > > Since you don't use "$" to dereference the value of awk variables (other > than $1, $2, etc.) then doing You seem to imply that 1, 2, etc. are identifiers of [special] variables that are an exception (compared to other variables) in being dereferenced by the $ operator. But the numbers are no special, they are just what is expected behind a $ operator; any expression that evaluates to a number. In the "A,K&W" Book the $ is thus named as "field" operator. It allows not only for expressions like $1 or $x, where x contains anything that evaluates to a number[*], but also $NF, $(NF-1), $ 1, $ x, $ f( 2 ), with any appropriate function f, or even $ /hi there/ . N.B.: I've tested the funny looking $/hi there/ application only with my GNU awk, so I'd be interested to hear from other awk's behaviour. Janis [*] Strings and booleans included, and, as shown, even regexp patterns. > > x=2 > printf "%s\n",x > printf "%s\n",$x > > in awk is equivalent to: > > x=2 > printf "%s\n" "$x" > eval printf '"%s\n"' \"\${$x}\" > > in shell IF the value of x is a number. If the value of x is not a > number then in awk $x is the same as $0. >[...]
From: Ed Morton on 30 Mar 2010 16:23 On Mar 30, 12:41 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> wrote: > Ed Morton wrote: > >>[...] > > > Since you don't use "$" to dereference the value of awk variables (other > > than $1, $2, etc.) then doing > > You seem to imply that 1, 2, etc. are identifiers of [special] variables > that are an exception (compared to other variables) in being dereferenced > by the $ operator. But the numbers are no special, they are just what is > expected behind a $ operator; any expression that evaluates to a number. I tend to think of them as variables since you can read and write them as you would variables but you are right. Thanks for pointing that out. > In the "A,K&W" Book the $ is thus named as "field" operator. It allows > not only for expressions like $1 or $x, where x contains anything that > evaluates to a number[*], but also $NF, $(NF-1), $ 1, $ x, $ f( 2 ), with > any appropriate function f, or even $ /hi there/ . > > N.B.: I've tested the funny looking $/hi there/ application only with my > GNU awk, so I'd be interested to hear from other awk's behaviour. Here's a few from Solaris: $ echo "hi there\nlo there" | gawk '{print NR,$/hi there/}' 1 hi 2 lo there $ echo "hi there\nlo there" | /usr/xpg4/bin/awk '{print NR,$/hi there/}' 1 hi 2 lo there $ echo "hi there\nlo there" | nawk '{print NR,$/hi there/}' nawk: syntax error at source line 1 context is {print >>> NR,$/ <<< hi there/} nawk: illegal statement at source line 1 $ echo "hi there\nlo there" | oawk '{print NR,$/hi there/}' awk: syntax error near line 1 awk: illegal statement near line 1 Regards, Ed. > Janis > > [*] Strings and booleans included, and, as shown, even regexp patterns. > > > > > > > x=2 > > printf "%s\n",x > > printf "%s\n",$x > > > in awk is equivalent to: > > > x=2 > > printf "%s\n" "$x" > > eval printf '"%s\n"' \"\${$x}\" > > > in shell IF the value of x is a number. If the value of x is not a > > number then in awk $x is the same as $0. > >[...]- Hide quoted text - > > - Show quoted text -
First
|
Prev
|
Pages: 1 2 Prev: building the printf format for a variable number of args Next: changing the second + in a bash script |