Extracting User Ids greater over 100 from the who command
I need to write a command to extract the user ids from the who command that are greater than 100 I came up with the following command, but I only sort the user ids.
who -all | sort -bnt ' ' -k 7.8,7 Thank You!! |
Hardly surprising - that's what sort is designed to do. You need to provide some arithmetic logic. Could be achieved in bash, but you'll find it much easier to use something that has real programming logic - awk, perl, python, lua, ...
|
As you are dealing with columns of data, I would probably use awk.
|
How about this one?
Code:
who | awk 's[$1]++==0 {print $1}' | xargs getent passwd | awk -F: '$3>100 {print $1}' Then these are looked up in passwd; xargs passes input lines as arguments to getent. The second awk prints the 1st field from passwd if the 3rd field (UID) is greater than 100. |
Or alternatively
Code:
who | cut -d ' ' -f1 | xargs -l id -u | cut -d ':' -f3 | grep ... | sort -u |
I really like the awk command from MadeInGermany, and the response from neonsignal. As you can tell, I am new to bash scripting and Linux. The cool thing here is that there are so many tools to get the job done. I did come up with the following:
who -l | grep 'id=[0-9][0-9][0-9]'| cut -c1-9,57-63 Which is the more accepted approach? Seems like using awk is less "awkward"? Which one is better? or is it just preference? Like I stated this is new to me. Thanks! |
Actually, the awk command used /passwd/ output instead of /who/ output. But it does make me want to learn /awk/ capabilities.
Shalom!!!! |
If you want to explore "awk" then figuring out how this pipeline works might be interesting.
Code:
(awk -F':' '$3 >= 100 { print $1 }' /etc/passwd; who | awk '!seen[$1] { print $1; seen[$1] = 1 }') | awk '{ fr[$1]++ } END { for(user in fr) if(fr[user] > 1) print user }' |
KISS generally works
Code:
who -all | awk '$7 > 100 {print $7}' | sort -u |
@syg00 - isn't that the pid for your terminal?
|
All times are GMT -5. The time now is 10:33 AM. |