I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!!
Every example of this has led me down a merry road and I'm wondering if anyone can actually explain this finer point to me?
I have found that the server I'm operating on has some anomolies in its implementation of UNIX so if I could possibly ask a favour of someone out there with awk skills...
would it be possible to write a small piece of example code 'which works' where a similar variable is imported and used in an awk function?
i ask this because I have every feeling that once again this code will work on every other unix installation and not the one we have here... if so I need to sort it out with the sys admin asap
but without some real working code as an example I won't have a chance of explaining.
So could anyone possibly run a working example past me?
thanks vish, its as I feared then and once again I find I've wasted a day because our beloved server is set up wrong...
my output from your code is
var=�hello�: command not found
var=�myname�: command not found
var2: undefined variable
and the output from your improvement on my line is
var: undefined variable
So I've sat here another full day trying to get things to work because our sys admin has rocks in his head... very sad and frustrating indeed.
thanks a million for that snippet of code, without which I couldn't properly explain the anomoly to the tech support and sys admins - so greatly appreciated.
I've had my brother in law look at this - its based around a university assignment... there are some basic stuff being fooled with like not being able to set variables
var="troy"
but i can put them in a text file and make them variables with a cat into the variable
anyway we've experimented and it appears the shell has been limited for some reason... in ksh
var="troy"
has no problem
so sorry for the bother everyone - this one seems to be something I need to take back to the lecturers. I've found ways to get around the limitations of what's going on but the time factor is killing me from the business end... had to knock back a good contract yesterday.
thanks for the help, it was crucial to my understanding and invaluable to my being able to successfully get past the hurdles.
no doubt i will have more questions on the weekend
in the bourne shell it accepts the var and var2 but the error at the end is
awk: syntax error near line 1
awk: bailing out near line1
I think typeset was another feature not working...
I really hope this is a professor trick and not an assignment where I'm expected to both learn shell scripting and decode what is and not a real part of the behaviour...
ah my bad that was rushed typing I meant #! at the beginning lol... I nearly missed the bus to uni.
Yes using solaris, which they pointed out to me this morning. I got around the problem instead by simply piping the variables through from the beginning
echo var1 var2 | awk blah blah
it seems the simple ways are after all the most prone to success, I do have a tendency to run off and look for a function to easily do stuff instead of looking first at basic stuff like echo (of all things).
thanks for the nawk tip - I'll be avoiding awk for some time I'd expect or at least until my burnt fingers stop throbbing.
thanks again all for offering help and advice, greatly appreciated.
A quick google search found the problem you're having. From
http://www.grymoire.com/Unix/Awk.html
there are 3 "versions" of awk:
AWK - the original from AT&T
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version
from the article:
"... all of AWK's features are in NAWK and GAWK. Most, if not all, of
NAWK's features are in GAWK. NAWK ships as part of Solaris. GAWK does
not. However, many sites on the Internet have the sources freely
available. If you user Linux, you have GAWK.
and
"... several UNIX vendors have replaced AWK with NAWK, and there are
several incompatibilities between the two."
So in asking your friends about this, they are no doubt using nawk or
gawk without realising, since it appears on their systems as awk.
On Solaris, nawk is a separate program to awk.
Your example code that fails with awk runs with nawk, since the
original awk doesn't support the "-v" command line parameter. I've
also checked with Dave about the problem you were having with the
percentage character on the end of a number, and again, nawk fixes
this problem.