For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2006 > Awk Script with multiple condition...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Awk Script with multiple condition...
bshah@citadon.com

2006-04-26, 6:57 pm

Hi Gurus,

With your help i created an awk script which looks as below:

awk '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total+=$1;print $2}
END { prin
t total " MB" > "/dev/tty" }' temp_file> result_file

temp_file
--------------
52 tst1
75 tst2
44 tst3
63 tst4

This gives all the names whose first record is between 50 and 375 and
second record starts with e-k or r-z. Now i need to add third condition
as:

UID=`ldapsearch -x -h ldap uid=$2 mailhost | grep -c mailHost`; if [
$UID -eq 1 ]

if UID value is 1 then only put it in result_file else reject that
entry.
I tried below:


awk '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ && UID=`ldapsearch -x
-h ldap uid=$2 mailhost | grep -c mailHost`; if [ $UID -eq 1 ] {
total+=$1;print $2} END { prin
t total " MB" > "/dev/tty" }' temp_file> result_file
Gives an error.
Please help me to resolve this script.
If you have better solution than this please do let me know.
Thanks a lot

Brian Inglis

2006-05-01, 3:58 am

On 26 Apr 2006 11:30:32 -0700 in comp.lang.awk, bshah@citadon.com
wrote:

>Hi Gurus,
>
>With your help i created an awk script which looks as below:
>
>awk '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total+=$1;print $2}
>END { prin
>t total " MB" > "/dev/tty" }' temp_file> result_file
>
>temp_file
>--------------
>52 tst1
>75 tst2
>44 tst3
>63 tst4
>
>This gives all the names whose first record is between 50 and 375 and
>second record starts with e-k or r-z. Now i need to add third condition
>as:
>
>UID=`ldapsearch -x -h ldap uid=$2 mailhost | grep -c mailHost`; if [
>$UID -eq 1 ]
>
>if UID value is 1 then only put it in result_file else reject that
>entry.


Try:

awk UIDS="`ldapsearch -x -h ldap uid=$2 mailhost | grep -c mailHost`"\
'$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ && UIDS == 1 { total+=$1;
print $2 }
END { print total " MB" > "/dev/tty" }' temp_file > result_file

Note after the closing `" there is a space before the opening ';
you can omit the \ and newlines and type this all as a single line;
also, awk UIDS="`...`" makes it an awk variable, only visible in awk;
UIDS="`...`" awk would make it an environment variable definition, but
not available in awk (just in case you thought that was a mistake).

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
fake address use address above to reply
bshah@citadon.com

2006-05-03, 6:57 pm

Hi Brian,
I tried but it doesn't understand uid=$2. uid value is blank which
means UIDS doesn't take temp_file as input to UIDS. Any idea how to
resolve this under awk?
Regards

> Try:
>
> awk UIDS="`ldapsearch -x -h ldap uid=$2 mailhost | grep -c mailHost`"\
> '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ && UIDS == 1 { total+=$1;
> print $2 }
> END { print total " MB" > "/dev/tty" }' temp_file > result_file
>
> Note after the closing `" there is a space before the opening ';
> you can omit the \ and newlines and type this all as a single line;
> also, awk UIDS="`...`" makes it an awk variable, only visible in awk;
> UIDS="`...`" awk would make it an environment variable definition, but
> not available in awk (just in case you thought that was a mistake).
>
> --
> Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
>
> Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
> fake address use address above to reply


Brian Inglis

2006-05-08, 7:56 am

On 3 May 2006 10:04:01 -0700 in comp.lang.awk, bshah@citadon.com
wrote:

[color=darkred]
>I tried but it doesn't understand uid=$2. uid value is blank which
>means UIDS doesn't take temp_file as input to UIDS. Any idea how to
>resolve this under awk?


Misread your original intent; try:
awk '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total + =$1;
cmd = "ldapsearch -x -h ldap uid=" $2 " mailhost | grep -c mailHost";
cmd | getline uids; close(cmd); if (uids == 1) print $2 }
END { print total " MB" > "/dev/tty" }' temp_file > result_file

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
fake address use address above to reply
bshah@citadon.com

2006-05-11, 6:56 pm

Hi Brian,
It gives syntax error:

awk: syntax error Context is:
[color=darkred]
> Misread your original intent; try:
> awk '$1 >= 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total + =$1;
> cmd = "ldapsearch -x -h ldap uid=" $2 " mailhost | grep -c mailHost";
> cmd | getline uids; close(cmd); if (uids == 1) print $2 }
> END { print total " MB" > "/dev/tty" }' temp_file > result_file
>
> --
> Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
>
> Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
> fake address use address above to reply


Ed Morton

2006-05-11, 6:56 pm

bshah@citadon.com wrote:

> Hi Brian,
> It gives syntax error:
>
> awk: syntax error Context is:
>
>


Make that += not + =

Please don't top-post.

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com