1752782 Members
6323 Online
108789 Solutions
New Discussion юеВ

Re: awk script

 
Gemini_2
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 9
Jeff_Traigle
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
Sundar_7
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 ?
Geoff Wild
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.
James R. Ferguson
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...
Gemini_2
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...
Andreas Voss
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
James R. Ferguson
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...
Geoff Wild
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.
Gemini_2
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!!