Operating System - Linux
1753785 Members
7633 Online
108799 Solutions
New Discussion юеВ

Looking for specific user ID's from the passwd file

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Looking for specific user ID's from the passwd file

Hello,

My issue is that I want to look for specific userids that have their first and last characters are alphabetic followed by four numbers.

I've already got the user ID's out of the passwd file:

more passwd | awk -F ":" '{print $1}' > userids.txt

I just need to know how to just pick the users out of the list that are formated:

aannnn

a=alphabet
n=number

like for example:

ab1234

Thanks in advance!

10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Looking for specific user ID's from the passwd file

Shalom,

awk has a print format function called printf

Take a look at that and you can do pattern matching.

You may also be able to use grep as a pre or post processor for this script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sandman!
Honored Contributor

Re: Looking for specific user ID's from the passwd file

# grep '^[a-z]\{2\}[0-9]\{4\}$' infile
James R. Ferguson
Acclaimed Contributor

Re: Looking for specific user ID's from the passwd file

Hi Patric:

# perl -aF":" -nle 'print $F[0] if $F[0]=~/[A-Za-z]+\d{4}$/' /etc/passwd

That is, split the field of the file on the ":" delimiter. If the first (zero-relative) field consists of one or more letters followed by exactly 4-digits, print the userid.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Looking for specific user ID's from the passwd file

Something like:

grep [a-z][a-z][0-9][0-9][0-9][0-9] userids.txt

should work.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Looking for specific user ID's from the passwd file

Hi (again) Patrick:

Steven > awk has a print format function called printf. Take a look at that and you can do pattern matching

Hardly, 'printf' does formatted printing not pattern matching.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Looking for specific user ID's from the passwd file

Pete's anwser was the closest:

I ended up doing this:

more passwd | awk -F ":" '{print $1}' | grep '[A-Za-z][A-Za-z][0-9][0-9][0-9][0-9]'
Sandman!
Honored Contributor

Re: Looking for specific user ID's from the passwd file

Yet another way to do it using awk(1):

# awk -F: '$1~"[a-z]{2}[0-9]{4}"{print $1}' /etc/passwd

~hope it helps
James R. Ferguson
Acclaimed Contributor

Re: Looking for specific user ID's from the passwd file

Hi Patrick:

> ended up doing this:

# more passwd | awk -F ":" '{print $1}' | grep '[A-Za-z][A-Za-z][0-9][0-9][0-9][0-9]'

That's a waste of two processes! Let 'awk' open and read the file instead of having 'cat' or 'more' spawn 'awk'. Then, let 'awk' do the matching that it was designed to do, eliminating the 'grep':

# awk -F":" '/[A-Za-z][A-Za-z][0-9][0-9][0-9][0-9]/ {print $1}' /etc/passwd

Regards!

...JRF...
Sandman!
Honored Contributor
Solution

Re: Looking for specific user ID's from the passwd file

Ignore last post and use the one below as it anchors the regular expression:

# awk -F: '$1~"^[a-z]{2}[0-9]{4}$"{print $1}' file

~cheers