awkpipe

Assigning system command's output to variable


I want to run the system command in an awk script and get its output stored in a variable. I've been trying to do this, but the command's output always goes to the shell and I'm not able to capture it. Any ideas on how this can be done?

Example:

$ date | awk --field-separator=! {$1 = system("strip $1"); /*more processing*/}

Should call the strip system command and instead of sending the output to the shell, should assign the output back to $1 for more processing. Rignt now, it's sending output to shell and assigning the command's retcode to $1.


Solution

  • Figured out.

    We use awk's Two-way I/O

    {
      "strip $1" |& getline $1
    }
    

    passes $1 to strip and the getline takes output from strip back to $1