LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extracting User Ids greater over 100 from the who command (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-user-ids-greater-over-100-from-the-who-command-4175577083/)

knowlgain 04-09-2016 08:38 PM

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!!

syg00 04-09-2016 09:25 PM

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

grail 04-10-2016 01:48 AM

As you are dealing with columns of data, I would probably use awk.

MadeInGermany 04-10-2016 06:18 AM

How about this one?
Code:

who | awk 's[$1]++==0 {print $1}' | xargs getent passwd | awk -F: '$3>100 {print $1}'
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.

neonsignal 04-10-2016 08:57 AM

Or alternatively
Code:

who | cut -d ' ' -f1 | xargs -l id -u | cut -d ':' -f3 | grep ... | sort -u

knowlgain 04-10-2016 09:47 AM

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!

knowlgain 04-10-2016 09:54 AM

Actually, the awk command used /passwd/ output instead of /who/ output. But it does make me want to learn /awk/ capabilities.


Shalom!!!!

cnamejj 04-10-2016 07:52 PM

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.

syg00 04-10-2016 08:30 PM

KISS generally works
Code:

who -all | awk '$7 > 100 {print $7}'  | sort -u

grail 04-10-2016 09:35 PM

@syg00 - isn't that the pid for your terminal?


All times are GMT -5. The time now is 10:33 AM.