If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > awk and simple if then

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-04, 16:01
shapi003 shapi003 is offline
Registered User
 
Join Date: Sep 2003
Location: Raleigh, NC
Posts: 6
Unhappy 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
Reply With Quote
  #2 (permalink)  
Old 04-07-04, 17:00
vgersh99 vgersh99 is offline
Registered User
 
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]);
}
Reply With Quote
  #3 (permalink)  
Old 04-07-04, 17:01
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,614
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
Reply With Quote
  #4 (permalink)  
Old 04-08-04, 04:36
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Quote:
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.
Reply With Quote
  #5 (permalink)  
Old 04-08-04, 08:36
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,614
Quote:
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On