LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-09-2016, 08:38 PM   #1
knowlgain
LQ Newbie
 
Registered: Apr 2016
Posts: 5

Rep: Reputation: Disabled
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!!
 
Old 04-09-2016, 09:25 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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, ...
 
Old 04-10-2016, 01:48 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As you are dealing with columns of data, I would probably use awk.
 
Old 04-10-2016, 06:18 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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.
 
Old 04-10-2016, 08:57 AM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Or alternatively
Code:
who | cut -d ' ' -f1 | xargs -l id -u | cut -d ':' -f3 | grep ... | sort -u
 
Old 04-10-2016, 09:47 AM   #6
knowlgain
LQ Newbie
 
Registered: Apr 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
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!
 
Old 04-10-2016, 09:54 AM   #7
knowlgain
LQ Newbie
 
Registered: Apr 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Actually, the awk command used /passwd/ output instead of /who/ output. But it does make me want to learn /awk/ capabilities.


Shalom!!!!
 
Old 04-10-2016, 07:52 PM   #8
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
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.
 
Old 04-10-2016, 08:30 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
KISS generally works
Code:
who -all | awk '$7 > 100 {print $7}'  | sort -u
 
Old 04-10-2016, 09:35 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
@syg00 - isn't that the pid for your terminal?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting root folder to a user home folder command mzein1292 Linux - Newbie 3 06-23-2015 07:26 AM
Command id Not Getting User / Group Names for the Corresponding IDs devUnix Linux - Server 2 08-17-2013 04:10 AM
Kindly advice the good linux mail server for 100 email ids by keeping the anti spam icleansystems Linux - Newbie 2 04-23-2009 09:07 AM
LXer: Red Hat sees FY07 greater China sales up 100 pct LXer Syndicated Linux News 0 09-26-2006 10:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration