Operating System - HP-UX
1753797 Members
7594 Online
108799 Solutions
New Discussion юеВ

Re: exporting variables from awk

 
SOLVED
Go to solution
Nicol├бs
New Member

exporting variables from awk

Hi there!

I'm trying to split a string in an unknown number of strings separated by spaces.

I'm trying with awk, but i can't export variables back to shell.

this is what i have, it doesn't work:

str='one two three four five six'
i=0
awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print a[i]; }' | \
while read name
do
filename[$i]=$name
echo ${filename[$i]}
let i=$i+1
done


I know i can append awk output in a file and then do
cat filename | \
while read name
....

but i don't want to use external files.

i also know about variable=`awk statement`
but this lastone can't return me more than one variable.

any idea?
14 REPLIES 14
Dennis Handly
Acclaimed Contributor

Re: exporting variables from awk

>but i can't export variables back to shell.
>it doesn't work:

You don't explain why it fails and what you want to do?

>I know i can append awk output in a file and then do

This is not much different than the pipe except you don't need a name.

>i also know about variable=$(awk statement)
but this last one can't return me more than one variable.

It returns you a string. Which you can fiddle with.

The standard trick is to return a command and use eval on it:
eval $(echo export x=1 b=2)

>str='one two three four five six'

You could use:
set -A filename $str

Or you could do:
for i in $str; do
echo $i
done


Mark Grant
Honored Contributor
Solution

Re: exporting variables from awk

Firstly it is not possible for anything to export variables to the parent shell (at least not without being extremely "hacky" about it).

To me, I think perl is your answer here but if you don't like it perhaps it would be simpler to change the "while" to a "for" loop and cycle through the output like this.

for $result in `awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print a[i]; }'`
do
filename[$i]=$result
etc etc etc
done

You might want to look at the venerable "expr" with a nice regular expression command instead of awk for something like this too.
Never preceed any demonstration with anything more predictive than "watch this"
Ralph Grothe
Honored Contributor

Re: exporting variables from awk

If you only need to populate an array of whitespace delimited tokens,
why the "awk" awkwardness?
The Posix shell can handle this

$ myvirtues="lust gluttony greed sloth wrath envy\npride"

$ set -A sins $(echo $myvirtues)

$ echo ${#sins[*]}
7

$ echo ${sins[*]}
lust gluttony greed sloth wrath envy pride
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: exporting variables from awk

Hi:

Adding a bit to Ralph's virtues:

# myvirtues=" myvirtues="lust gluttony greed sloth wrath envy\npride\nimpatience\nlaziness\nhubris"

# echo ${myvirtures}|xargs
lust gluttony greed sloth wrath envy pride impatience laziness hubris

Regards!

...JRF...
Nicol├бs
New Member

Re: exporting variables from awk

Ralph and James:
that works great on ksh, but unfourtunly i'm working on bash.

i'll explain what i need
what i have to do is to look for a file and do something with it.
points are:
-I've to search in many directories
-I'll probably found not one but two, three or four files, with the same name but diferent extension. and perhaps some completly similar files in diferent directories
-I have to chose only one of those, which one? the one has not null lenght (wc -l) and the one has the higher value in a list of extension priorities

so, my idea was to loop the dirs, and make finds with the only file mask for any dir. then get the find result in a variable, split it, and make stuff.

but perhaps that's not the best idea.
time ago "somebody" used to do it like this:

file_1=`echo $fullname | cut -d' ' -f1`
file_2=`echo $fullname | cut -d' ' -f2`

but he didn't concern into the posibility have more than two filenames in the string.
Dennis Handly
Acclaimed Contributor

Re: exporting variables from awk

>that works great on ksh, but unfortunately i'm working on bash.

(Scripts should be in sh or ksh.)

>file_1=`echo $fullname | cut -d' ' -f1`
>but he didn't concern into the possibility have more than two filenames in the string.

If they are whitespace separated then "for" or arrays should automatically separate them.
Ralph Grothe
Honored Contributor

Re: exporting variables from awk

Hi Nicolas,

is your bash run on Linux or HP-UX?

Btw, bash can equally well handle arrays
like hpux-sh or ksh.
It only has a slightly deviating syntax
(a peek at man bash always helps ;-)

Because we haven't installed Bash on our HP-UX boxes (simply no need for it there)
I am running this on Linux.

$ uname
Linux


$ echo $BASH_VERSION
3.1.17(1)-release


To stick with James' extended set of virtues
(btw. I would have thought that laziness was covered by sloth, and hubris by pride?)

$ myvirtues=" myvirtues="lust gluttony greed sloth wrath envy\npride\nimpatience\nlaziness\nhubris"

as I'm doing this on Linux I need to cater the escape option along with the echo command (omit on hpux)

$ sins=($(echo -e "$myvirtues"))

$ echo ${#sins[*]}
10

$ echo ${sins[*]}
lust gluttony greed sloth wrath envy pride impatience laziness hubris


But this doesn't help you much, I suppose?

Apropos, the GNU find available on Linux
has a nifty -print0 option,
which separates the output records by \0
so that find can also cope with (Unix)unusual file names that contain whitespace etc.
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: exporting variables from awk

Hi (again):

And I would add that the use of 'xargs' applies to the Bash environment too:

$ virtues=" laziness\timpatience hubris\nare\nvirtures"
$ echo "${virtues}"
laziness impatience hubris
are
virtures
$ values=$(echo "${virtues}"|xargs)
$ echo ${values}
laziness impatience hubris are virtures

By the way, I teasingly used "hubris ~ pride" and "laziness ~ sloth" as Ralph pointed out, since these are Perl virtues he appreciates too :-)

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: exporting variables from awk

Hi (again):

> Ralph: Apropos, the GNU find available on Linux has a nifty -print0 option, which separates the output records by \0 so that find can also cope with (Unix)unusual file names that contain whitespace etc.

HP-UX introduced this to 11.23 with patch: PHCO_32269.

The 'find' command for 11.31 supports this option, too.

Regards!

...JRF...