LinuxQuestions.org
Visit Jeremy's Blog.
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-27-2015, 05:24 AM   #1
Red_Templar
LQ Newbie
 
Registered: Apr 2015
Posts: 2

Rep: Reputation: Disabled
Searching for a specific username


Hey guys,

I've recently started learning Linux, and I was wondering if anyone can tell me how I can search for a specific usernames starting with a certain letter on a Linux system.

For example: How do I get it to display all the usernames starting with a J? (So that it would show me, Jack, Jason, John, etc...)
I tried to find the answer online, but haven't had much luck so far.
 
Old 04-27-2015, 03:29 PM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by Red_Templar View Post
Hey guys,

I've recently started learning Linux, and I was wondering if anyone can tell me how I can search for a specific usernames starting with a certain letter on a Linux system.

For example: How do I get it to display all the usernames starting with a J? (So that it would show me, Jack, Jason, John, etc...)
I tried to find the answer online, but haven't had much luck so far.
All usernames are in /etc/password. So you can grep for lines in that file that start with a j in the following way:
Code:
grep -i ^j /etc/passwd
The -i flag is for the pattern matching to be case insensitive
If you want to only output the actual username, pipe that output into cut:
Code:
grep -i ^j /etc/passwd | cut -d':' -f 1
 
Old 04-27-2015, 03:58 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
Quote:
Originally Posted by joe_2000 View Post
If you want to only output the actual username, pipe that output into cut
Or use awk?
Code:
awk -F':' '/^j/ {print $1}' /etc/passwd
 
Old 04-27-2015, 04:22 PM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by unSpawn View Post
Or use awk?
Code:
awk -F':' '/^j/ {print $1}' /etc/passwd
Don't know why, but for some reason I seem to be bad at remembering the awk syntax. I found that most of times I can get away with pipe and cut.

Maybe I should use awk more often to get better at it. But then, luckily, many ways lead to Rome ;-)
 
Old 04-28-2015, 02:07 AM   #5
Red_Templar
LQ Newbie
 
Registered: Apr 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys, both ways worked great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Searching a specific directory for files containing a text string lothwen Linux - Newbie 1 11-20-2010 10:58 AM
problem searching by username lurker79 LQ Suggestions & Feedback 6 05-05-2006 07:09 AM
Searching a specific directory for a specific extension? RoaCh Of DisCor Linux - Newbie 3 08-13-2005 03:28 PM
CGI script: username specific hamish Linux - Software 1 01-17-2005 08:00 AM
Searching username in a password file Linix Linux - Newbie 5 09-15-2003 04:20 PM

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

All times are GMT -5. The time now is 12:38 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