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!

Better way of accessing an AWK output 2

Status
Not open for further replies.

moonring

Technical User
Feb 15, 2008
38
US
Hi,

Let's assume in a shell script we have an awk code that does processing of input, and after it has been processed inside AWK, I need to make that value accessible outside of AWK for further processing in the script.
Currently I'm using a temporary file to store that value to have it later available outside of AWK, which does the job, but I'm not too happy with that.

So I'd like to know if there is a more elegant, neater way of declaring that value outside of AWk, without using a temp file, but maybe using an AWK native tool which I'm not aware of ?


Example:

Code:
#!/bin/sh

ls -l | awk 'NR==1{print $NF > "temp"}'

cat temp
....
further processing here of temp file....
...
rm temp

Thanks,
 
Since it seems you are only processing a small amount of output, simply assign it to a variable:

Code:
#!/bin/sh

file=`ls -l | awk 'NR==1 { print $NF }'`

echo $file
...

Or, if you only need to process the output data once, pipe it in to the next step of the process.

Annihilannic.
 
I probably should have said:

Code:
echo "$file"

That way it would preserve white space and new lines, should you actually wish to process multiple lines of output.

Annihilannic.
 
Yes, that makes sense, and I use that way (assigning the awk output to a variable) extensively for short AWK commands as shown above, but I was thinking more in the lines of a more massive AWK code in a script with many values of input and output, and you want to access particular values of AWK output at certain points in the script.
Anyway it seems that I have to go with either methods described here, not a big problem.

Thanks again Annihilannic,

Appreciate your help.
 
You may use the eval shell builtin, eg:
Code:
set -x;eval `ls -l | awk 'NR>1{print "file"NR-1"="$NF}'`;set +x

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for coming back to this subject again, because it still is kind of open question for me.

Your approach is exactly what I was hoping to find and I think I have a final answer to this issue:

The use of variables instead of temporary files in assigning many awk outputs for further use in the script.

So the code:

Code:
#!/bin/sh

echo '345..' | awk '{split("345...",a,"")
                print a[1] > "value01"
                print a[2] > "value02"
                print a[3] > "value03"
                            }'

if [ `cat value01` -gt 1 ]
then
 ...do something
fi
if [ `cat value02` -gt 2 ]
then 
... do something else ...
fi
if [ `cat value03` -gt 3 ]
then
.... other actions ...
fi
....
rm value*

becomes the following code, (which works great):

Code:
#!/bin/sh

eval $(echo '345..' | awk '{split("345...",a,"")
                print "value01""="a[1], "value02""="a[2] , "value03""="a[3] }' )                
                                          
if [ "$value01" -gt 1 ]
then
 ...do something
fi
if [ "$value02" -gt 2 ]
then 
... do something else ...
fi
if [ "$value03" -gt 3 ]
then
.... other actions ...
fi
....

Just to permanently confirm, using eval is the final and the best solution to this issue ?

Thanks for your time
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top