Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find unique userid's from passwd file 3

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
I need help with a script to find only users that starts with a alpha character followed by 4 digits (ie. a9999).

Here's what I did so far (not working):

#!/usr/bin/ksh
cat /etc/passwd | awk -F: '{print $1}' |
while [[ $MORE -ge 1 ]]
do
read USER
if [ len(USER) == 5 ]
then
if [substr(USER) == "[a-z][0-9][0-9][0-9][0-9]"]
echo " * found : $USER"
fi
fi
done
 
What about this ?
Code:
awk -F: '$1~/^[a-z][0-9][0-9][0-9][0-9]$/{print $1}' /etc/passwd

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That worked. You are the best PHV!
Thanks!
 
PHV,

It works as a one liner, but I get an error when I put it in a script:

#!/usr/bin/ksh
ssh server1 awk -F":" '$1!~/^[a-z][0-9][0-9][0-9][0-9]$/{print $1}' /etc/passwd
Syntax Error The source line is 1.
The error context is
>>> !~ <<<
awk: 0602-540 There is a missing } character.
awk: 0602-500 Quitting The source line is 1.
 
When you send commands over ssh you have to protect them from being "quote processed" by the local shell.

Try this:

Code:
#!/usr/bin/ksh
ssh server1 'awk -F":" "\$1!~/^[a-z][0-9][0-9][0-9][0-9]\$/{print \$1}" /etc/passwd'

Note that I had to surround the entire command passed via ssh in single quotes, and convert any quotes between them into double quotes. I also had to escape any $ characters to prevent them from being substituted with variable values by the shell on the remote system. I hope it works, because I didn't test it. :)

Annihilannic.
 
Annihilannic,
Yes, it worked just as you said.
Your help is greatly appreciated.
A big star for you.
pdtak.
 
Thanks Annihilannic!
It worked just as you explained.
A big star for you!
pdtak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top