Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Location
    Raleigh, NC
    Posts
    6

    Unhappy Unanswered: awk and simple if then

    Hello,

    I am trying make an awk script that does a command, looks at $10. If $10 does not have a value, print $9, else print $10. I think this is probably fairly easy, but I am getting lost doing this. Can somebody please show me how to do this?

    Thanks,

    David

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    {
        cmd="whatEverCommand2execute"
        cmd | getline cmdOutput; close(cmd);
        split(cmdOutput, cmdARR);
         # don't know exactly what "doesn't have a value" means
        printf("%s\n", (cmdARR[10] =="") ? cmdARR[9] : cmdARR[10]);
    }

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I haven't used awk in years, but I'm 99% sure that you can use:
    Code:
    printf($10 ? $10 : $9);
    to print $10 if it is defined and $9 otherwise.

    -PatP

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Originally posted by Pat Phelan
    I haven't used awk in years, but I'm 99% sure that you can use:
    Code:
    printf($10 ? $10 : $9);
    to print $10 if it is defined and $9 otherwise.

    -PatP
    Don't use printf because the first argument is a format.
    If the printed field ($9 or $10) contains a valid directive (eg %d) awk will failed.
    Code:
    print ($10 ? $10 : $9);
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Originally posted by aigles
    Don't use printf because the first argument is a format.
    If the printed field ($9 or $10) contains a valid directive (eg %d) awk will failed.
    Code:
    print ($10 ? $10 : $9);
    Uff-da! Good catch, sorry. I still think you just need to add the format string and then it should work fine, or at least it does on my awk.

    -PatP

Posting Permissions

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