Re: awk script (197 Views)
Reply
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 1 of 10 (197 Views)

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
Please use plain text.
Honored Contributor
Jeff_Traigle
Posts: 1,354
Registered: ‎03-04-2004
Message 2 of 10 (197 Views)

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
Please use plain text.
Honored Contributor
Sundar_7
Posts: 1,938
Registered: ‎11-07-2000
Message 3 of 10 (197 Views)

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 ?
Please use plain text.
Honored Contributor
Geoff Wild
Posts: 7,168
Registered: ‎06-04-2001
Message 4 of 10 (197 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (197 Views)

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...
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 6 of 10 (197 Views)

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...
Please use plain text.
Honored Contributor
Andreas Voss
Posts: 1,364
Registered: ‎10-14-1997
Message 7 of 10 (197 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 10 (197 Views)

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...
Please use plain text.
Honored Contributor
Geoff Wild
Posts: 7,168
Registered: ‎06-04-2001
Message 9 of 10 (197 Views)

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.
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 10 of 10 (197 Views)

Re: awk script

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

system("cleartool lsvob" vob) }'


I assigned everyone some points!!!!

thank you again!!

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation