Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting variables from awk

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Is there a way to export vars from an awk script without having to write their values to a file? I've read the other threads concerning this problem and the answers have been to do something like this: VarName='echo var =...'.
But my script prints text to stdout, so I don't think that this would work... Any suggestions?

- Tobias
(tobbe_snodd@hotmail.com)
 
Tobias-

I am assuming you are invoking awk from a shell script.

If so, and all you want is to display the value/s of
vars used in the awk program, then you can use one
of these methods to write to stdout:

awk '

var1=1
var2=3
var3=var1 + var2

print var3

message = sprintf("This is the total: %s\n", var3)
print message

printf("%s\n", var3)

All of which will simply print to stdout.

The awk program can be embedded within the shell script
or called as an external file. Either way you'll get
the result written to stdout.

If my assumptions are correct, then it's easy!

Hope this helps.



flogrr
flogr@yahoo.com

 
-flogrr

Thanks for your help, but that wasn't really what I meant. I've written a shellscript which sends a couple of vars to an awkscript. They are changed by awk and then I want to get them back to the shellscript, not write them to stdout. I'm sorry that I didn't explain it properly...

By the way... is it possible to send an array from a shellscript to be used in awk without having to write it to a file?


- Tobias
(tobbe_snodd@hotmail.com)
 
Here's a miserable hack to do what you're asking:

#!/bin/sh

##### Untested #####

VARA="foo"
VARB="bar"

NAWK="/usr/bin/nawk"
CUT="/usr/bin/cut"
ECHO="/usr/bin/echo"

TMP=`$NAWK -v vara=$VARA -v varb=$VARB '
{
# Do something useful here
}
END
{
# Use whatever separator suits your data
print vara ":" varb;
}' name_of_input_file`

VARA=`$ECHO $TMP | $CUT -f1 -d":"`
VARB=`$ECHO $TMP | $CUT -f2 -d":"`

Sure ain't pretty, maybe somebody else can come up with something more elegant ;-)

As to the array issue, I don't know. There _may_ be versions of awk that have a mechanism for accessing shell script arrays, check your version's docs.

You could also put something together that concatanates the contents of the array into one variable which you feed to the awk script.

HTH,



Russ
bobbitts@hotmail.com
 
Hi Tobias,

Sorry, couldn't reply any sooner.

You can pass the vars into nawk / gawk from within a shell
wrapper something like this:

var1="word" # or var1=$1 when passed in by call to
# the shell script from command line

Then, inside nawk it is used like this '"$var1"' where
needed. The quotes sometimes need to be reversed to
work for both text and numeric data. (I can never get
it right the first time it seems!)

Sometimes it helps if you build shell pipelines made up
of shell commands, filters like sort, etc., awk scripts,
and even sed scripts to manipulate data.

You may be able to pipe your shell array out of the
command or application that created it and into an awk
script, process through awk, and pipe out into another
command or even stdout. It depends if it's on-the-fly
processing or not.

The output from your awk script can thus be piped into
another command, captured in a shell variable, or displayed
on stdout.

Structure like this:

# shell script wrapper

var1="one"
var2="two"

<command that generates data> |
nawk '

{ ... do something with vars '&quot;$var1&quot;' .... '&quot;$var2&quot;' }

END{ print .... }' | < pipe, redirect, etc. here > ...

You could possibly pipe in the array, manipulate the vars,
and pipe out the results all in one pipeline.

The example that Russ gave sets the results of the awk
script to a var named TMP, then goes from there when
focus is again back in the shell. Several ways to do
things- just need to experiment with the various tools.

This may get you going in a positive direction!

Jesse


flogrr
flogr@yahoo.com

 
set `awk 'BEGIN{ print &quot;Hello regards doei&quot; }'`
echo $1 $2 $3 $#

output from echo -> Hello regards doei 3
3 is the $# means 3 fields.

when you have a large awk script:
set `awk 'BEGIN{
etc...
etc...
}'`
echo ...

Regards Gregor.weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top