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
> 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
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
> 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.
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.
> 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
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
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
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.