Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138

    Unanswered: unix command using in awk

    Hi,
    I want to execute a system call in awk and store the result in a awk variable. I get the result from the command, but not the output which is given from it.

    for example :

    awk ......
    x=system("date")

    I need a solution in awk !!! and not by using shell in this form :

    DATE=´date´
    awk -v DATE=$DATE .....

    Thanx for your interest.
    Greetings from germany
    Peter F.

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can execute a command and read the result with 'getline' using the syntax :
    Code:
    command | getline [variable]
    The following awk script read commands, execute them and print result.

    Code:
    awk '
    function ExecCmde(cmde    ,line, result) {
       while ((cmde | getline line) > 0) {
          result = result (result=="" ? "" : "\n") line ;
       }
       return result;
    }
    tolower($1) ~ /exit|quit/ {
       exit;
    }
    NF>0 {
       print "===",$0,"===";
       print ExecCmde($0);
       print "===";
    }
    '
    Jean-Pierre.

  3. #3
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    a little correction, add this line in the function before 'return'
    Code:
    close(cmde);
    Jean-Pierre.

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Thanx aigles,
    it is a very nice solution, and it works very fine when I type my "date" command followed by CR . Exit or quit ends the script. (wonderfull).
    It is realy the way I want to do.
    But I dont understand the source code . Is there an easier way, without typing the command. For example the "date" command as a fix text ???

    Thanx a lot
    Greetings from germany
    Peter F.

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    For a command that return a signgle line (like 'date') command, you can do :

    Code:
      "date" | getline the_date;
      close("date");  
    print the_date;
    
    # The same, with command in a variable
      date_cmde = "date +'%d.%m.%Y'";
      date_cmde | getline the_date;
      close(date);
      print the_date;
    If your command return multiple line, you get them within a loop (like in my previous post) :

    Code:
       while ((cmde | getline line) > 0) {
          print line;
       }
       close(cmde);
    Jean-Pierre.

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Peter,

    You can also use the 'system' subroutine to explicitly call native utilities...

    awk -v cmd=date 'BEGIN{system(cmd)}'

  7. #7
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Thanx Jean-Pierre,

    it works well . Thank you for your interest to my problem.


    Thanks Damian too, I have learn a lot with your comment.

    but, when I try this :
    awk -v cmd="date +%d%m%Y" 'BEGIN{
    system(cmd)
    x=system(cmd)
    print x
    }'

    the printout from variable x, there is a second line with "0"

    look at the output which is given :
    24022004
    24022004
    0
    Greetings from germany
    Peter F.

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    when you use 'system' function to execute an external command, you don't get the output of the command. What you get is the status.

    x=system(date)
    execute the command, output goes to stdout, and x is set to status of command (0 for succes);

    print x
    print the status of the execution of the 'date' command.

    if you want to retrieve the output of the command, you must use the 'command | getline' method.
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •