Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variable inside awk

778 views
Skip to first unread message

Wolfy

unread,
Jul 15, 2011, 4:41:39 AM7/15/11
to
Hi!

I have an awk script where a replace the newline character with some
text like this:
[$] awk '{ str1=str1 $0 "mytext"}END{ print str1 }' /path/to/file.txt

Now I would like to replace "mytext" with a variable, and have tried
that following that gives me an error:
[$] mytext="thistext"
[$] awk -v mytext=$mytext '{ str1=str1 $0 $mytext}END{ print str1 }' /
path/to/file.txt

Would be great if someone had a little hint on how I could solve this!

Thanks!

Wolf

pk

unread,
Jul 15, 2011, 4:48:46 AM7/15/11
to
On Fri, 15 Jul 2011 01:41:39 -0700 (PDT)
Wolfy <wolfh...@gmail.com> wrote:

> Hi!
>
> I have an awk script where a replace the newline character with some
> text like this:
> [$] awk '{ str1=str1 $0 "mytext"}END{ print str1 }' /path/to/file.txt
>
> Now I would like to replace "mytext" with a variable, and have tried
> that following that gives me an error:
> [$] mytext="thistext"
> [$] awk -v mytext=$mytext '{ str1=str1 $0 $mytext}END{ print str1 }' /
> path/to/file.txt

I don't think that should give you an error, rather "only" unexpected
results (unless $mytext contains more complex text, in which case you may
indeed get errors at the shell level)

> Would be great if someone had a little hint on how I could solve this!

[$] mytext="thistext"
[$] awk -v mytext="$mytext" '{ str1=str1 $0 mytext}END{ print str1 }' /path/to/file.txt

Wolfy

unread,
Jul 15, 2011, 5:21:41 AM7/15/11
to
On Jul 15, 10:48 am, pk <p...@pk.invalid> wrote:
> On Fri, 15 Jul 2011 01:41:39 -0700 (PDT)
>

By changing $mytext to mytext in the command itself as in your
example,
I got this to work on Linux (GNU Awk 3.1.5) as well as on OS X (awk
version 20070501).

Using $mytext like this worked in Linux (though as I you said:
unexpected results), but I got an error in OS X:
[$] awk -v mytext="$mytext" '{ str1=str1 $0 $mytext}END{ print
str1 }' /path/to/file.txt

The error in OS X was:
awk: illegal field $(thistext), name "mytext"

Thanks for help and quick answer! :-)

Wolf

pk

unread,
Jul 15, 2011, 5:27:48 AM7/15/11
to
On Fri, 15 Jul 2011 02:21:41 -0700 (PDT) Wolfy <wolfh...@gmail.com> wrote:

> Using $mytext like this worked in Linux (though as I you said:
> unexpected results), but I got an error in OS X:
> [$] awk -v mytext="$mytext" '{ str1=str1 $0 $mytext}END{ print
> str1 }' /path/to/file.txt
>
> The error in OS X was:
> awk: illegal field $(thistext), name "mytext"

Not sure what that error means exactly, but don't use $mytext inside awk.
The awk variable is called "mytext".

Ed Morton

unread,
Jul 15, 2011, 7:17:07 AM7/15/11
to

I suspect the variable "mytext" had the value "thistext" and "thistext" is not a
valid field number (as opposed to 1, 2, etc.).

Most awks would just convert the string "thistext" to the number 0 (like in any
other string->number conversion scenario such as an arithmetic operation on a
string) and use "$0", e.g. gawk:

$ echo "a b c" | awk '{mytext="2"; print mytext, mytext+0, $mytext}'
2 2 b
$ echo "a b c" | awk '{mytext="thistext"; print mytext, mytext+0, $mytext}'
thistext 0 a b c

but it's not inconceivable that some awks might instead give you a warning or
error message if you try to access a field using a non-numeric string.

For the OP - awk is not shell. It's syntax is more like C, so to access the
contents of a variable just use the variable name like you would in C, no need
to stick a "$" in front of it. "$N" means field number "N" in the input record.

Ed.

pk

unread,
Jul 15, 2011, 7:22:38 AM7/15/11
to
On Fri, 15 Jul 2011 06:17:07 -0500, Ed Morton <morto...@gmail.com> wrote:

> I suspect the variable "mytext" had the value "thistext" and "thistext"
> is not a valid field number (as opposed to 1, 2, etc.).
>
> Most awks would just convert the string "thistext" to the number 0 (like
> in any other string->number conversion scenario such as an arithmetic
> operation on a string) and use "$0", e.g. gawk:
>
> $ echo "a b c" | awk '{mytext="2"; print mytext, mytext+0, $mytext}'
> 2 2 b
> $ echo "a b c" | awk '{mytext="thistext"; print mytext, mytext+0,
> $mytext}' thistext 0 a b c
>
> but it's not inconceivable that some awks might instead give you a
> warning or error message if you try to access a field using a non-numeric
> string.

Ah that makes sense. The awk on Mac OS isn't gawk indeed. Thanks

Kenny McCormack

unread,
Jul 15, 2011, 8:21:28 AM7/15/11
to
In article <ivp7jh$rhg$1...@dont-email.me>,

Ed Morton <morto...@gmail.com> wrote:
>On 7/15/2011 4:27 AM, pk wrote:
>> On Fri, 15 Jul 2011 02:21:41 -0700 (PDT) Wolfy<wolfh...@gmail.com> wrote:
>>
>>> Using $mytext like this worked in Linux (though as I you said:
>>> unexpected results), but I got an error in OS X:
>>> [$] awk -v mytext="$mytext" '{ str1=str1 $0 $mytext}END{ print
>>> str1 }' /path/to/file.txt
>>>
>>> The error in OS X was:
>>> awk: illegal field $(thistext), name "mytext"
>>
>> Not sure what that error means exactly, but don't use $mytext inside awk.
>> The awk variable is called "mytext".
>>
>
>I suspect the variable "mytext" had the value "thistext" and "thistext"
>is not a
>valid field number (as opposed to 1, 2, etc.).
>
>Most awks would just convert the string "thistext" to the number 0 (like in any
>other string->number conversion scenario such as an arithmetic operation on a
>string) and use "$0", e.g. gawk:
>
>$ echo "a b c" | awk '{mytext="2"; print mytext, mytext+0, $mytext}'
>2 2 b
>$ echo "a b c" | awk '{mytext="thistext"; print mytext, mytext+0, $mytext}'
>thistext 0 a b c
>
>but it's not inconceivable that some awks might instead give you a warning or
>error message if you try to access a field using a non-numeric string.

Yes, I can confirm that that is the behavior under MacOS. Of course, it
isn't really MacOS that is the culprit; it's the BSD-ish Unix ancestry from
which MacOS derives that is at fault.

Clearly a bug; here is a workaround:

print $(foo+0)

Yes, both the parens and the +0 are necesary.

--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus

Hermann Peifer

unread,
Jul 15, 2011, 11:02:46 AM7/15/11
to
On 15/07/2011 13:17, Ed Morton wrote:
> On 7/15/2011 4:27 AM, pk wrote:
>> On Fri, 15 Jul 2011 02:21:41 -0700 (PDT) Wolfy<wolfh...@gmail.com>
>> wrote:
>>
>>> Using $mytext like this worked in Linux (though as I you said:
>>> unexpected results), but I got an error in OS X:
>>> [$] awk -v mytext="$mytext" '{ str1=str1 $0 $mytext}END{ print
>>> str1 }' /path/to/file.txt
>>>
>>> The error in OS X was:
>>> awk: illegal field $(thistext), name "mytext"
>>
>> Not sure what that error means exactly, but don't use $mytext inside awk.
>> The awk variable is called "mytext".
>>
>
> I suspect the variable "mytext" had the value "thistext" and "thistext"
> is not a valid field number (as opposed to 1, 2, etc.).
>
> Most awks would just convert the string "thistext" to the number 0 (like
> in any other string->number conversion scenario such as an arithmetic
> operation on a string) and use "$0", e.g. gawk:
>
> $ echo "a b c" | awk '{mytext="2"; print mytext, mytext+0, $mytext}'
> 2 2 b
> $ echo "a b c" | awk '{mytext="thistext"; print mytext, mytext+0, $mytext}'
> thistext 0 a b c
>
> but it's not inconceivable that some awks might instead give you a
> warning or error message if you try to access a field using a
> non-numeric string.
>

Gawk actually does both, if one enables lint warnings:

$ echo "a b c" | /usr/bin/awk --lint '{mytext="thistext"; print mytext,
mytext+0, $mytext}'
awk: (FILENAME=- FNR=1) warning: attempt to field reference from
non-numeric value

Hermann

Ed Morton

unread,
Jul 17, 2011, 8:00:01 AM7/17/11
to

Interesting. I would've thought that was something that'd be useful to have
enabled by default, but I wouldn't want this warning:

$ echo "a b c" | awk --lint '{fld="1"; print $fld}'


awk: (FILENAME=- FNR=1) warning: attempt to field reference from non-numeric value

a

to be the default just because the initial assignment to "fld" used a string.

A warning when "fld" doesn't contain an integer would be useful though so we get
warned about trying to reference fields using non-numeric strings and
non-integer numbers like this:

$ echo "a b c" | awk --lint '{fld=1.3; print $fld}'
a

Regards,

Ed.

opedroso

unread,
Oct 30, 2011, 6:15:17 PM10/30/11
to
Had the same problem for a few days now, after coming back to awk after many years away.

The AHA! moment comes when one realizes that after an awk variable has been assigned, you get to its contents by using its name. E.g.

proc = $3
print proc # and not "print $proc" like you would need to in a shell language

Hope this helps somebody else along their journey.
0 new messages