For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > AWK, condition other then AND ??









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, condition other then AND ??
Krzysiek

2005-11-12, 6:55 pm


/usr/local/bin/awk -F":" --assign=grp=$GET_grupa --assign=login=$GET_login -
-assign=name=$GET_imie --assign=uid=$GET_uid --assign=gid=$GET_gid --assign=
surname=$GET_nazwisko 'split($5,ar,"[ ,]") && ($1==login && $4==grp &&
toupper(ar[1])==toupper(name) && toupper(ar[2])==toupper(surname) ) {print
$0"<br>"}' /etc/passwd

Hello

That AWK program displays only lines that match AND condition as you can see
&&, &&
but $GET_login can be empty and lets say $GET_grupa is set to 5000 (awk
grp=5000). In this case awk will not return anything - $1 (login field in
/etc/passwd) can not be empty and awk will not
return anything (no match $1==NULL). I would like it to return all lines
that
match group ($4), even when other fileds are empty. How shoud I do it?

TIA

(if you saw my previous post about AWK and CGI - do not answer it, I have
managed to work around that)

William James

2005-11-12, 6:55 pm

Krzysiek wrote:
> /usr/local/bin/awk -F":" --assign=grp=$GET_grupa --assign=login=$GET_login -
> -assign=name=$GET_imie --assign=uid=$GET_uid --assign=gid=$GET_gid --assign=
> surname=$GET_nazwisko 'split($5,ar,"[ ,]") && ($1==login && $4==grp &&
> toupper(ar[1])==toupper(name) && toupper(ar[2])==toupper(surname) ) {print
> $0"<br>"}' /etc/passwd
>
> Hello
>
> That AWK program displays only lines that match AND condition as you can see
> &&, &&
> but $GET_login can be empty and lets say $GET_grupa is set to 5000 (awk
> grp=5000). In this case awk will not return anything - $1 (login field in
> /etc/passwd) can not be empty and awk will not
> return anything (no match $1==NULL). I would like it to return all lines
> that
> match group ($4), even when other fileds are empty. How shoud I do it?
>
> TIA


BEGIN {
FS = ":"
grp = ENVIRON[ "GET_grupa" ]
login = ENVIRON[ "GET_login" ]
name = ENVIRON[ "GET_imie" ]
surname = ENVIRON[ "GET_nazwisko" ]
}

split($5,ar,"[ ,]") && (""==login || $1==login) && $4==grp &&
toupper(ar[1])==toupper(name) && toupper(ar[2])==toupper(surname){
print $0 "<BR>"
}

Ed Morton

2005-11-13, 3:55 am

William James wrote:

> Krzysiek wrote:
>
>
>
> BEGIN {
> FS = ":"
> grp = ENVIRON[ "GET_grupa" ]
> login = ENVIRON[ "GET_login" ]
> name = ENVIRON[ "GET_imie" ]
> surname = ENVIRON[ "GET_nazwisko" ]
> }


The above only works for exported shell variables or variables set on
the command line when you invoke awk, so you're better off using one of
the shell->awk variable styles recommended in the comp.unix.shell FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24).

Ed.

news.t-online.de

2005-11-13, 3:55 am

Krzysiek wrote:
> /usr/local/bin/awk -F":" --assign=grp=$GET_grupa --assign=login=$GET_login -
> -assign=name=$GET_imie --assign=uid=$GET_uid --assign=gid=$GET_gid --assign=
> surname=$GET_nazwisko 'split($5,ar,"[ ,]") && ($1==login && $4==grp &&
> toupper(ar[1])==toupper(name) && toupper(ar[2])==toupper(surname) ) {print
> $0"<br>"}' /etc/passwd
>
> Hello
>
> That AWK program displays only lines that match AND condition as you can see
> &&, &&
> but $GET_login can be empty and lets say $GET_grupa is set to 5000 (awk
> grp=5000). In this case awk will not return anything - $1 (login field in
> /etc/passwd) can not be empty and awk will not
> return anything (no match $1==NULL). I would like it to return all lines
> that
> match group ($4), even when other fileds are empty. How shoud I do it?
>
> TIA
>
> (if you saw my previous post about AWK and CGI - do not answer it, I have
> managed to work around that)
>


This does not surprise. split() has a return value. In case the string
to be split (here $5) is empty split returns 0, this then is logically
resolved to false in the complete logical AND expression. This is not
really environment variable related nor really awk specific.

echo "hello" | awk '{print split("",a)}'
0

split($5,ar,"[ ,]")
$5 in /etc/passwd is not necessarily filled and represents
Gecos. Specifies general information about the user that is not needed
by the system, such as an office or phone number. The value is a
character string. The Gecos field cannot contain a colon.



'(split($5,ar,"[ ,]") + 1) && ($1==login && $4==grp &&
toupper(ar[1])==toupper(name) && toupper(ar[2])==toupper(surname) )
{print $0"<br>"}' /etc/passwd
should do it, it is always >0 and so
alwas true and the print out condition only depends on
those logical terms you really want to have.

Sponsored Links







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

Copyright 2008 codecomments.com