1833175 Members
2726 Online
110051 Solutions
New Discussion

Re: awk helppp

 
SOLVED
Go to solution
zap_2
Advisor

awk helppp

Hi unix gurus,
If I want to do like this:
#who -T|awk '$6!=192.120.20.107 and $1=root {print $9}'
Why this script does not work.Thanks in advance
15 REPLIES 15
RAC_1
Honored Contributor

Re: awk helppp

who -T|awk '{if($6!=192.120.20.107) && ($1=root)print $9}'
There is no substitute to HARDWORK
zap_2
Advisor

Re: awk helppp

Still not work ...
Muthukumar_5
Honored Contributor
Solution

Re: awk helppp

who -T | awk '($6!="192.120.20.107") && ($1=="root") {print $9}'

hth.
Easy to suggest when don't know about the problem!
James R. Ferguson
Acclaimed Contributor

Re: awk helppp

Hi:

If you mean to print the last field, then 'awk' counts fields one-relative. The logical 'and' operator is also '&&'. Did you mean:

# who -T|awk '$8!~/192.120.20.107/ && $1~/root/ {print $9}'

...or for the last field of the line:

# who -T|awk '$8!~/192.120.20.107/ && $1~/root/ {print $NF}'

Regards!

...JRF...

zap_2
Advisor

Re: awk helppp

Why the above script print all user root even if this user have ip 192.120.20.107??
Hein van den Heuvel
Honored Contributor

Re: awk helppp



This works for me:

who -T | awk '/^root/&& !/192.120.20.107 / {print $NF}'

it looks for a line starting with 'root' and not containing a specific IP address anywhere in the line.

please note that the 'dots' in the ip address are in fact regular expression elements for 'any' char. So if the line contained 192X120Y20Z107 then it woudl also be disgarded, but this seems reasonable considering the intended use.


hein.
Muthukumar_5
Honored Contributor

Re: awk helppp

Which script is making problem? Please specify with example line and execution result to get back promptly.

hth.
Easy to suggest when don't know about the problem!
James R. Ferguson
Acclaimed Contributor

Re: awk helppp

Hi:

--> "Why the above script print all user root even if this user have ip 192.120.20.107??".

The answer is that you negated (with "!") the IPaddress, so you asked for all logins that do NOT match that address but (and) are 'root' users.

Regards!

...JRF...
Muthukumar_5
Honored Contributor

Re: awk helppp

>> Why the above script print all user root even if this user have ip 192.120.20.107??

All solutions stated above are negating 192.120.20.107 IP-Address and checking with root user also. Check with example and let us know If you find same problem again.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: awk helppp

Try with perl also as,

who -T | perl -ne '$ip=(split)[8] if !/root.*192.120.20.107/;print $ip . "\n";

hth.'
Easy to suggest when don't know about the problem!
zap_2
Advisor

Re: awk helppp

I wan to print user root except root with ip 192.120.20.107 ....how to do this??
James R. Ferguson
Acclaimed Contributor

Re: awk helppp

Hi (again):

--> I wan to print user root except root with ip 192.120.20.107

Then one way is:

# who -T|awk '$1~/^root/ && $NF!~/192.120.20.107/ {print $0}'

...use print $0 to see the whole line or if you only want the last field, use print $NF.

Regards!

...JRF...
zap_2
Advisor

Re: awk helppp

If I want to print all user root except root for ip 192.120.20.107 and 192.120.20.108.How to change this script??Thanks
James R. Ferguson
Acclaimed Contributor

Re: awk helppp

Hi:

--> If I want to print all user root except root for ip 192.120.20.107 and 192.120.20.108.How to change this script?

# who -T|awk '$1=="root" && $NF!="192.120.20.107" && $NF!="192.120.20.108" {print $0}'

...is one way. I have changed from using regular expressions to string comparisons for exact matching of the fields. Thus, if you had "root" and "rootuser" this would only match "root" in field #1. This was one point that Hein had made.

You could also do:

# who -T|awk '$1=="root" && $NF~/192\.120\.20\.10[78]/ {print $0}'

This last one uses a match of the last field with a regular expression that matches EITHER of the IP address YOU WANT while avoiding the ambiguity of the dot character too.

Regards!

...JRF...

zap_2
Advisor

Re: awk helppp

Thanks all gurus .....