Operating System - HP-UX
1830230 Members
2271 Online
109999 Solutions
New Discussion

awk and system built-in function

 
SOLVED
Go to solution
Mauro Gatti
Valued Contributor

awk and system built-in function

Hi All, do You know if awk can runs system commands (using 'system' built-in function)giving as argument of this command the value of his internal variables?

Thank You
Ubi maior, minor cessat!
4 REPLIES 4
Jean-Luc Oudart
Honored Contributor

Re: awk and system built-in function

Hi

If I understand your question you want to build the command within awk script.

You can use :
buf=sprintf("%s %s",var1,var2);
and then
system(buf);

Rgds,
Jean-Luc
fiat lux
Mauro Gatti
Valued Contributor

Re: awk and system built-in function

I'd like to do:

if input line is > alpha beta gamma delta

awk should execute an "chown gamma:beta alpha"

Ubi maior, minor cessat!
Jean-Luc Oudart
Honored Contributor
Solution

Re: awk and system built-in function

awk '
{
if () {
buf=sprintf("chown %s:%s %s",$3,$2,$1);
system(buf);
}
}'

Rgds,
Jean-Luc
fiat lux
RolandH
Honored Contributor

Re: awk and system built-in function

Hi Mauro,

a simple awk script what will do this is
# cat chown.awk

BEGIN { system("chown " ARGV[1] ":" ARGV[2] " " ARGV[3]) }


ie
onwer: badman
group: marvel
file : comic

than do this

# awk -f chown.awk badman marvel comic

That's it.

HTH
Roland
Sometimes you lose and sometimes the others win