How to pass values between awk and shell scripts

I know that we can call system command to execute shell script in awk.

but it does not return the result of the command executed , but only returns
the value of the command executoin status ( 1/0 --> failure / success).

Could anyone let me know how to solve this problem.

check this
http://www.unix.com/showthread.php?p=302094516\#post302094516

Below example will execute a shell command, and ...

  1. Read cmd output into current line.

  2. Read cmd output into awk variable.

"shell cmd" | getline
2.
"shell cmd" | getline var

Good Luck !
KW

Hi,

I have the same problem and I the solutions posted here don't work for me or I don't understand them :frowning: So, how can I retrive in awk the returned value by a shell command and not its return status (0,1)?

My problem: I want to perform some date manipulation inside awk. For this, I want to convert the dates in the number of seconds using the date shelll command.

cmd=sprintf("date -d %s +%%s",d1);
val=system(cmd);

The problem is that val receives 0 (success) and not the number of seconds.

Thanks a lot

Isi

As described in this thread....

system(cmd) | getline val
close(cmd)

Hi

Thanks for the reply but I get an error when using getline. Is getline an standard UNIX command (I'm using cygwin :-()? Can I use it inside an awk code?

My code is

awk -v inicio=$f1 -v fin=$f2 '
function compareDate(d1,d2)
{

  cmd=sprintf("date -d %s +%%s",d1);
  system(cmd)|getline s1;
  close(cmd);
  cmd=sprintf("date -d %s +%%s",d2);
  system(cmd)|getline s2;
  close(cmd);

  if (s1 > s2)
  {
    retVal=1;
  }
  else if ( s1=s2 )
  {
    retVal=0;
  }
  else
  {
    retVal=-1;
  }


  return retVal;
}

doprint==0 && compareDate($1,inicio)>=0 { doprint=1; }
doprint==1 && compareDate($1,fin)>=0    { exit; }
doprint==1 { print;  }

When I execute it, s1 and s2 are always empty and what I see in stdout is the ouput of the 'date' commands

Thanks a lot again

Isi

sprintf("date -d %s +%%s",d1)

This actually expects TWO parameters passed to it - you're just passing ONE (d1). Where's the second?
Do you get anything spilled to stderr complaining about 'sprintf'?
What's this '+%%s' supposed to be?

Hi

%% is used for escaping the special character %, so %%s wouldn't expect any command ant it will print the % character.

sprintf("date -d %s +%%s",d1) 

will print

date -d <value for d1> +%s

which is the command I need for converting the date in format
mm/dd/yyyy in a number (number of seconds) to be able for performing
comparations between dates (which date goes after the oter....)

For example, you can try this code

awk -v d1=03/20/2009 '
BEGIN { msg=sprintf("date -d %s +%%s",d1);
        print msg;
        system(msg);
      }'

The output will be

date -d 03/20/2009 +%s
1237503600

So, as it is expected, the probhlem is that I can't capture the value 1237503600 in awk for performing my calculations.

Thanks

Isi

Here is an example. All code goes into your awk script ...

  1. The value of the current line's $1 will be inserted into the cmd variable.
  2. The string in the cmd variable will be executed in the unix shell by awk's getline, and the command's output will be saved in var (a newly created awk variable).
  3. Awk will have access to the command's output via the var variable.
   cmd="echo "$1" | tr [:upper:] [:lower:]"	
   cmd | getline var
   close(cmd)
   print var

�� THANKS A LOT!! :slight_smile:

It works fine, I didn't know I could execute directly the UNIX commando without system.

Thanks again for all yout help!!!

Isi