Awk Script with multiple condition...

  • Follow


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

0
Reply bshah (26) 4/26/2006 6:30:32 PM

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]ab[dot]ca)
    fake address		use address above to reply
0
Reply Brian 5/1/2006 5:10:51 AM


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]ab[dot]ca)
>     fake address		use address above to reply

0
Reply bshah 5/3/2006 5:04:01 PM

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

>> 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).

>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]ab[dot]ca)
    fake address		use address above to reply
0
Reply Brian 5/8/2006 12:51:39 PM

Hi Brian,
It gives syntax error:

awk: syntax error  Context is:
>>>     = 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total + =       <<<

> 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]ab[dot]ca)
>     fake address		use address above to reply

0
Reply bshah 5/11/2006 8:41:09 PM

bshah@citadon.com wrote:

> Hi Brian,
> It gives syntax error:
> 
> awk: syntax error  Context is:
> 
>>>>    = 50 && $1 <= 375 && $2 ~ /^[e-kr-z]/ { total + =       <<<
> 

Make that += not + =

Please don't top-post.

	Ed.
0
Reply Ed 5/11/2006 10:56:36 PM

5 Replies
544 Views

(page loaded in 0.21 seconds)

Similiar Articles:













7/25/2012 5:08:34 PM


Reply: