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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk...

Status
Not open for further replies.

nithink

Programmer
Nov 7, 2002
92
US
Hi,
In the below given awk statement I want to assign the $3 to a variable.. ur help appreciated...

awk '{if ($3 != "") print "get " $3}' ./files.txt >> ./tmp5

 
Sorry.. I didnt mention properly in the question i asked.. I want to assign the $3 to a variable so that I need to use that variable in the remaining part of script.. can yu pls let me know.. ur help appreciated...

awk '{if ($3 != "") print "get " $3}' ./files.txt >> ./tmp5



 
Well..
You can do this:

function getthree() {
mfn=$1
fn=$2
out=`awk -v fn=$fn ' {
if ($3) {
print $3 ; print $3 >> fn
close(fn)
}
}' $mfn`
test ! -z $out && export out && return $?
}
Now all you have to do is call;
getthree filename tmpfilename

YMMV

Good Luck
 
hi marsd,
thx for your reply.. am new to scripting and 've never used a function call.. can u pls explain me how do u call this fn ? and can u explain this line
getthree filename tmpfilename ( i know this is the call to the function.. but dont know whats that tmpfilename and filename... ur help appreciated...
 
To use the function, call it as written at the point in your script where you would do your awk {print $3}, etc.. line

The first parameter is the name of the file you wish awk to read from,the second parameter is the tmpfile you want
results written to. The result of reading field three is
exported into your shell global environment if not empty and can be accessed as the variable out:ex: echo $out.

ex:
getthree /home/myfile /home/tmp.out
echo $out

 
do you realy have to do that with awk?

while read xx
do
set $xx
if [ -n "$3" ] ;then
echo $3
fi
done < files.txt >> ./tmp5

can you do it like this?

regards Gregor Gregor.Weertman@mailcity.com
 
Do you really have to do it with the shell?
Did the OP ask for a shell solution?

I don't know. I don't even work here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top