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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2006
Registered User
 

Join Date: Mar 2006
Posts: 77
Exclamation awk compound statements

how can i use two or multiple statements in the if part
of an awk code

for example
i want to check two flag if they are true i will write some print
operations and increase the counter.
here is the c version of the code that i want to write:

counter=0;
if (flag1==1 && flag2==0) {
printf("true\n");
counter=counter+1;
flag1=0;
}
else
printf("false");


how can i do the same operation using awk?
Sponsored Links
  #2 (permalink)  
Old 07-13-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,195
just like you said.......
  #3 (permalink)  
Old 07-14-2006
Registered User
 

Join Date: Mar 2006
Posts: 77
Exclamation

i dont think that, its just like i said.
(at least i couldnt make it work)

let me ask my question with a different example.
i wrote an awk code as follows,
and i cannot make it work, my guess is: the problem stems from
the compund statements. whats wrong with this code?

for file in TESTFILE*; do
counter1=0;
counter2=0;
awk -F" " '{ if ( $1 = /flag1/ ) { print "TRUE"; $2="true"; counter1=$counter1+1}
else { print "FALSE"; counter2=$counter2+1 }}' $file>testfileoutput
done
print $counter1
print $counter2
  #4 (permalink)  
Old 07-14-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Awk tests in the form


Code:
$1 == "foo" && $2 == "bar"

are perfectly acceptable, as is

Code:
$1 ~ /^foo$/ && $2 ~ /^bar$/

Check out http://www.vectorsite.net/tsawk2.html#m3. That should help you.
  #5 (permalink)  
Old 07-14-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,195
Quote:
Originally Posted by gfhgfnhhn
i dont think that, its just like i said.
(at least i couldnt make it work)

let me ask my question with a different example.
i wrote an awk code as follows,
and i cannot make it work, my guess is: the problem stems from
the compund statements. whats wrong with this code?

for file in TESTFILE*; do
counter1=0;
counter2=0;
awk -F" " '{ if ( $1 = /flag1/ ) { print "TRUE"; $2="true"; counter1=$counter1+1}
else { print "FALSE"; counter2=$counter2+1 }}' $file>testfileoutput
done
print $counter1
print $counter2
if you're hoping to see SHELL vars being incremented and exported from 'awk' and being seen in SHEEL after the end of the loop... that is NOT how it works.

Also your increment [counter1=$counter1+1] withIN the awk is NOT what you THINK it's supposed to do.


Code:
#!/bin/ksh
for file in TESTFILE*; do
    counter1=0;
    counter2=0;
    eval $(nawk -v out='testfileoutput -v counter1="${counter1}" -v counter2="${counter2}" '{ 
          if ( $1 = /flag1/ ) { 
             print "TRUE" > out 
             $2="true";
             counter1++
          }
          else {
             print "FALSE" > out
             counter2++
          }
     }
     END {
         printf("export counter1=%d\nexport counter2=%d\n", counter1, counter2)
     }' $file )
done
print $counter1
print $counter2


Last edited by vgersh99; 07-14-2006 at 01:02 PM..
  #6 (permalink)  
Old 07-15-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Quote:
Originally Posted by vgersh99
if you're hoping to see SHELL vars being incremented and exported from 'awk' and being seen in SHEEL after the end of the loop... that is NOT how it works.

Also your increment [counter1=$counter1+1] withIN the awk is NOT what you THINK it's supposed to do.


Code:
#!/bin/ksh
for file in TESTFILE*; do
    counter1=0;
    counter2=0;
    eval $(nawk -v out='testfileoutput -v counter1="${counter1}" -v counter2="${counter2}" '{ 
          if ( $1 = /flag1/ ) {

Perhaps $1 ~ /flag1/.
Quote:
Code:
             print "TRUE" > out 
             $2="true";

After this assignment, is $2 or $0 ever used?
Quote:
Code:
             counter1++
          }
          else {
             print "FALSE" > out
             counter2++
          }
     }
     END {
         printf("export counter1=%d\nexport counter2=%d\n", counter1, counter2)
     }' $file )
done
print $counter1
print $counter2

  #7 (permalink)  
Old 07-17-2006
Registered User
 

Join Date: Mar 2006
Posts: 77
Exclamation

thanks for your replies
but there is one more thing that i want to ask

eval $(nawk -v out='testfileoutput -v counter1="${counter1}" -v counter2="${counter2}" '

why i cannot use the above line as follows:

eval $(nawk '-v out=testfileoutput -v counter1="${counter1}" -v counter2="${counter2}" '


i moved the first single quote to just after nawk, before -v
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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compound command with 'find' utility? deckard Shell Programming and Scripting 6 10-05-2005 05:27 PM
Compound indirect variable references tkrussel UNIX for Advanced & Expert Users 5 08-21-2005 11:03 AM
Trying to use 'compound variable' in a script irina Shell Programming and Scripting 1 01-29-2004 10:33 PM
Trying to use 'compound variable' in a script neemic Shell Programming and Scripting 3 01-16-2004 07:07 AM
compound expression in unix spalmer UNIX for Dummies Questions & Answers 3 10-19-2001 01:04 PM



All times are GMT -4. The time now is 08:19 AM.


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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0