Results 1 to 6 of 6

Thread: awk test script fails to print variables

  1. #1
    Join Date
    Jan 2015
    Beans
    16

    awk test script fails to print variables

    In learning to use awk, I came across this example script, and copy-pasted to a txt file to test it out:

    Code:
    #!/bin/awk -f
    
         BEGIN {
    
        for (i=1; i <= 10; i++) {
            printf "The square of ", i, " is ", i*i;
        }
    
    exit;
    }
    But I get this from cli:

    Code:
    matias@...:gradebooks$ awk -f printsquares.awk
    The square of The square of The square of The square of The square of The square of The square of The square of The square of The square of

  2. #2
    Join Date
    Apr 2012
    Beans
    7,257

    Re: awk test script fails to print variables

    awk's 'printf' expects a C-style format string followed by arguments

    Code:
            printf "The square of %d is %d\n", i, i*i;
    or just change 'printf' to the simpler 'print'

  3. #3
    Join Date
    Feb 2008
    Location
    Oblivion
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: awk test script fails to print variables

    Thread moved to Programming Talk.

  4. #4
    Join Date
    Mar 2011
    Beans
    141
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: awk test script fails to print variables

    If you are using the hashbang (which for me is /usr/bin/awk) you should just be able to execute the file directly (having made it executable) as in:

    Code:
    $ ./printsquares.awk

  5. #5
    Join Date
    Jan 2015
    Beans
    16

    Re: awk test script fails to print variables

    Problems solved! Thanks, guys.

  6. #6
    Join Date
    Feb 2008
    Location
    Oblivion
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: awk test script fails to print variables

    Good news. Please mark thread as solved to help future travelers. See the second link in my signature for how. Thanks.

Bookmarks

Posting Permissions

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