Using shell variables In awk

Oh its not my day for syntax...

cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt

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?

let's say there is a previous

var=filecontent

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?

possibly

var=varname

echo 10 12 | awk 'BEGIN {VAR=var}{print "something",$1,$2,$VAR}'

or something similar?

I think you want this:

var="hello"
var2="myname"

echo $var2 | awk -v variable=${var} '{ print variable,$1}'

Output:
hello myname

your code shoule be

echo 10 12 | awk -v VAR=${var} '{print "something",$1,$2,VAR}'

Output:
something 10 12 hello

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

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 think i need to go to bed now.

what shell are you using? Most of the examples you got used /usr/bin/sh or /usr/bin/bash or /usr/bin/ksh.
Please show output from:

usr/bin/echo "$SHELL"
usr/bin/echo "$0"

these are csh/tcsh responses

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 :slight_smile:

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

thanks again

also,

in my script I believe the first line

!#/bin/sh

is supposed to put me into the bourne shell???

It doesn't as I'm staying in tcsh

!#/bin/sh

should be

#!/bin/sh

if you're running on Sun/Solaris.... try using 'nawk' instead of plain/old 'awk'

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.

I just got an email from the uni as to what the issue turned out to be... out of interest I will post it for the rest.

-------------------------------------------------------

Hi Stephen,

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.

Hope that's enough to get you going.

Cheers,

Tony