BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

awk script

Regular Advisor

awk script

I have the following awk script

list="apple:fruit
dog:animal"

for i in $list;
do
echo $i |awk -F: '{ vob =$1; vobpath=$2;
"unix-command vob" }'
done

I want to run a unix-command within awk, can it be done?

how do I let the unix command know "vob" is not literal "vob", but a variable?

thank you
9 REPLIES
Honored Contributor

Re: awk script

A quick look at the awk man page reveals there is a system(cmd) function that executes cmd and returns its exit code.
--
Jeff Traigle
Honored Contributor

Re: awk script

Will this work for you ?

# echo "$list" | awk -F: '{print $1,$2}' | xargs -n1 unix-command
Learn What to do ,How to do and more importantly When to do ?
Honored Contributor

Re: awk script

Why not:

list="apple:fruit
dog:animal"

for i in `echo $list |awk -F: '{ print $1}'`
do
unixcommand $i
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Acclaimed Contributor

Re: awk script

Hi:

I'm not sure exactly what you want to run, but you can do things like this:

# echo "hi:there" |awk -F: '{print $1;system("echo ok|xargs")}'

# echo "hi:there" |awk -F: '{print $1;system("date")}'

Regards!

...JRF...
Regular Advisor

Re: awk script

I like the system suggestion, other suggestions are also good. But, my unix command has a hard time taking the variable from awk..see below


for i in $list;
do
echo $i |awk -F: '{ vob =$1; vobpath=$2;
system("cleartool lsvob vob") }'
done

it interpret vob as "vob" instead of a variable...
Highlighted
Honored Contributor

Re: awk script

Hi,

try this:

for i in $list
do
echo $i |awk -F: '{ vob=$1; vobpath=$2;
system("cleartool lsvob" vob) }'
done

Regards
Acclaimed Contributor

Re: awk script

Hi Gemini:

Here's a better example:

# cat ./runner
#!/usr/bin/awk -f
{
system("echo ok " $1)
}

echo "Gemini" | ./runner

Notice that the command to run and its fixed arguments are enclosed in double quotes. The variable data is not.

Regards!

...JRF...
Honored Contributor

Re: awk script

Another way:

#!/bin/sh
list="apple:fruit
dog:animal"

for i in $list;
do
vob=`echo $i |awk -F: '{print $1}'`
echo $vob
done

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Regular Advisor

Re: awk script

thanks for everyone's kind help!
I got it!

system("cleartool lsvob" vob) }'


I assigned everyone some points!!!!

thank you again!!