Extracting User Ids greater over 100 from the who command
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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, ...
The first awk prints the 1st column and discards duplicates.
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.
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.
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 }'
I think use "who -l" and pulling what you need from it (as posted in an earlier response) sounds like the cleanest option. But grokking how the commands posted above work should give you a few new "awk" tricks you can use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.