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!

Variable from awk to shell? 2

Status
Not open for further replies.

fouh

Programmer
Sep 10, 2002
1
FR
Hi,
I have a awk program which changes the value of a shell variable and I don't know how to get this values in the shell.
I know how to do this whitn a file but I 'd prefer whith a variable(cleaner).
Exemple:
"
varshell='OK'
awk NR==1
'{
if NF==2...
varshell='NOK...
.
.
} file
.
.
print $varshell

"
Result OK or NOK
 
try

eval variable=`awk ...` ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
why 'eval'? No need for that!

Do a simple 'print' and use an 'active function' for nawk

#-------- test.sh
#!/bin/ksh
a="OK"
echo "Before-> [${a}]"

a=$(nawk -f myAWK.awk)

echo "After-> [${a}]"
#--------------------------------

# --------- myAWK.awk
BEGIN {
foo="BAD"
print foo;
}
#-------------------
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
ya, considere something like:

POLLO=&quot;AAA BBB CCC&quot;
eval `echo $POLLO | sed -e 's/[^ ]*/&=&/g'`
echo ..$AAA..$BBB..$CCC..

it's not just a game, i really needed it. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
who's on first? ;)

Yours is sed. OP was asking an awk question. If you want to do similar in awk you'd still need to 'print', but print with the same '=' construct to be used by the 'eval'.

Yours simply assumes different paradigm. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
thanks vlad.
i find you a litte superfflously acid-tongued.
you mybe should know how (the fantastic) awk and sed works, to understand sed is faster:

AWK: reads the file, split it in memory and works on this.
if the file is small and|or you have a lot of mem, you will
not see this.

SED: read in memory the sed-cmd (limited to 200)
then read a line at time from input file, an apply the
commds on this line.

as long you don't have aritmeticals compounding statements awk will NEVER get a chance to be faster then sed.

the example above, is an example !
this is more appropriate:

QQQ=`exec something, and assume it returns (or prints) 3 results, separated by blancs`

eval `echo $QQQ | sed -e 's/\(.*\)[ ]*[ ]\(.*\)[ ]*[ ] \(.*\)/AA=\1 BB=\2 CC=\3/'`

echo $AA
echo $BB
echo $CC

you sure also know, you don't need the QQQ= step
eval 'exec-cmd | sed ....'
echo ...

please excuse my english, this is not my preferred language.
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
sorry i forgot [ ]*[ ] between \2 and CC

why use a tool IF AN OTHER tool can better do this specific job ??
unix is as flexible as people using it are. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
jamisar,
I don't understand WHY you're trying to prove something to me so rigorously when I simply never challenged your point of view and simply stated your approach to be of the 'different paradigm'. We're in 'violent agreement' - relax.

We're a friendly bunch here @tek-tips.com vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
sorry vlad, i am not 'violent' and i feel you attack me (in ther threads)
i work in a big bisness in switzerland
every week we replace gigs-of-code with gigs-of-code
to save (milli)secunds
reading the new code i find (this shit):
aa='cat file|grep aa|wc -l|awk '{print $1;}''
bb='cat file|grep bb|wc -l|awk '{print $1;}''
and so on until the value != zz
NOTA: no function, just repeat it!!

i try to explain:

a) analize the problem
b) try the rigth tool to solve the problem
c) awk|sed|what ever you want|c|perl my be the rigth one
d) use it!

don't use a tool only because you know this tool, my be there is a better tool to do this job.
that's all. :)

i personnaly, have no problems with you.
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
> sorry vlad, i am not 'violent' and i feel you attack me (in ther threads)

that's a missconception [to say the least].

[snip-snap]

> don't use a tool only because you know this tool, my be there is a better tool to do this job.

Sorry, but I don't agree with that. &quot;The best tool for the job is the one you know best&quot; [(c)common sense wisdom, USENET].

Either way, this is OT and belongs to a different forum.

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top