The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
egrep syntax question DKNUCKLES Shell Programming and Scripting 1 11-18-2007 04:46 PM
yet another awk field syntax question prkfriryce Shell Programming and Scripting 4 03-22-2007 01:13 PM
C-shell: variable syntax question alex_5161 SUN Solaris 0 01-30-2007 02:43 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM
awk syntax question hcclnoodles Shell Programming and Scripting 2 10-28-2004 12:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2007
natdeamer natdeamer is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
Simple Awk Question: If Syntax

what is the correct syntax for an if statement:

1. {} around the code that you want to run, if the if is true
{if (($34 != "") && (NR != 1) && ($1 != "F"))
{
is_number(34,"S1002a")
less_than(34, 0, "S1002a")
}
}

2. or with no {}
{if (($34 != "") && (NR != 1) && ($1 != "F"))
is_number(34,"S1002a")
less_than(34, 0, "S1002a")
}

Thanks
  #2 (permalink)  
Old 09-10-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Consider this following code:
Code:
$ cat data2
6 sjjs
8 akakaka
9 akakaka
$ awk '{ if ($1 == 8) print NR, $0 }' data2
2 8 akakaka
$
So the "if" statement works without "{}" around the "print NR, $0" statement. But it also works with "{}":
Code:
$ awk '{ if ($1 == 8) {print NR, $0} }' data2
2 8 akakaka
$
If this case the "{}" is pretty useless. But if we want two statements to depend on the "if" condition, then we really need those "{}" to group the statements.

Code:
$ awk '{ if ($1 == 8) print NR, $0 ; print "------" }' data2
------
2 8 akakaka
------
------
$ awk '{ if ($1 == 8) {print NR, $0 ; print "------"} }' data2
2 8 akakaka
------
$
We probably wanted the second result.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0