Hello,
My issue is that I want to look for specific users that have their first and last initial followed by four numbers. For example:
ab1234
I've already got the user ID's out of the passwd file
more passwd | awk -F ":" '{print $1}' > userids
I just need to know how to just pick the users out of the list that are formated:
aannnn
a=alphabet
n=number
Thanks in advance!
waiq
2
from the top of my head. I think you can use somthing like this:
grep -e ^[:alpha:][:digit:]*
Thanks for the replay. Will that account for all the different userids that fit that pattern I mentioned above?
EDIT: I tried the following:
more passwd | awk -F ":" '{print $1}' | grep -e ^[:alpha:][:digit:]
and got a few users I didn't want like:
lp
adm
and some other users who didn't fit the EXACT pattern I asked for above.
I want to be able to list all users that fit the pattern below;
aannnn
example:
ab1234
more passwd | awk -F ":" '{print $1}' | grep '[A-Za-z][A-Za-z][0-9][0-9][0-9][0-9]'
Unbeliever. That was the perfect answer! Thanks. I learn more everyday.
more passwd | awk -F ":" '{print $1}' | egrep '[A-Za-z]{2}[0-9]{4}'
Is slightly more compact but gives you exactly the same thing. Since the user id is the first field in the password file you could simply do:
egrep '^[A-Za-z]{2}[0-9]{4}:' /etc/passwd
egrep '^[A-Za-z]{2}[0-9]{4}:' /etc/passwd
Your egrep prints out not only the userids, but the other stuff after the first ":" also. Doesn't work.
Yet another method I found:
awk -F: '$1~"^[a-z]{2}[0-9]{4}$"{print $1}' /etc/passwd
Or (assuming GNU grep):
printf "%.6s\n" $(egrep -o '^[[:alpha:]]{2}[0-9]{4}:' /etc/passwd)
Or with GNU sed:
sed -nr 's/^([[:alpha:]]{2}[[:digit:]]{4}):.*/\1/p' /etc/passwd
Otherwise:
sed -n 's/^\([A-Za-z]\{2\}[0-9]\{4\}\):.*/\1/p' /etc/passwd
Oops :o
Should of been:
egrep '^[A-Za-z]{2}[0-9]{4}:' /etc/passwd | awk -F':' '{print $1}'
or
egrep '^[A-Za-z]{2}[0-9]{4}:' /etc/passwd | sed -e 's/:.*//'