1827886 Members
1554 Online
109969 Solutions
New Discussion

Script user list

 
SOLVED
Go to solution
MSwift
Regular Advisor

Script user list

Good Morning!
This is in continuation to
http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1359353. Thanks to JRF and Dennis for helping me out. However it not capturing all the ID's. Here is what i exactly want. ID's like jk4567 (2 leters and 4 nos) and jk456e (2 letters, 3 nos and one letter). ONLY these 2 combinations need to be filtered and passed to the command, here is the current script..

logins -u | while read USERID X
do
echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && ${USERID}
done

Please help.

Best Regards

Mike.

10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: Script user list

Hi Mike:

For the 'grep' substitute:

grep -q -E '^[a-zA-Z]{2}[0-9]{3}[a-zA-Z0-9]$'

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Script user list

Just fwiw,

Please consider doing the filter before the read.

logins -u | grep -E "^[... " | while ..

It is not important here, assuming a low volume or rows (hundreds or thousands).

But for an application selecting thousands amongst millions of rows this order will avoid forking a grep process for every single row. That may be important for some usages, and just feels better for all.

Cheers,
Hein.

MSwift
Regular Advisor

Re: Script user list

JRF

Thanks, gave 7 points to keep this going (worth 100 :)). i found another grep, your comments please

grep ^[a-z][a-z][0-9][0-9][0-9][0-9,a-z]: /etc/passwd|cut -d: -f1

One more question JRF, now what if i want to EXCLUDE jk4567 and jk456e, meaning i want to run the some_command on all id's except the type jk4567 and jk456e? (only these type , exactly 6 characters in this combination only)

Thanks again

Best Regards

Mike
James R. Ferguson
Acclaimed Contributor

Re: Script user list

Hi (again) Mike:

> i found another grep, your comments please

> grep ^[a-z][a-z][0-9][0-9][0-9][0-9,a-z]: /etc/passwd|cut -d: -f1

This looks for entries in '/etc/passwd' that begin (the ^) with two lowercase letters, followed by 3-digits, followed by either a digit or a lowercase letter and a ":". If that match is satistifed, then the first colon-delimited field is snipped (cut) from the stream. TMTOWTDI. I would have used 'awk' to match and print what I wanted in ONE process.

> One more question JRF, now what if i want to EXCLUDE jk4567 and jk456e, meaning i want to run the some_command on all id's except the type jk4567 and jk456e? (only these type , exactly 6 characters in this combination only)

One way to do this is to test for these exact matches and if seen, skip the loop:

logins -u | while read USERID X
do
[ "${USERID}" = "jk4567" ] && continue
[ "${USERID}" = "jk456e" ] && continue
echo ${USERID} | grep -q -E '^[a-zA-Z]{2}[0-9]{3}[a-zA-Z0-9]$' && ${USERID}
done

Regards!

...JRF...
MSwift
Regular Advisor

Re: Script user list

JRF-

Thanks (again), I think i presented my question wrongly, what i wanted was, i wanted run the cammonds on all ID's except of the format jk4567 (2 letters, 4 nos) and of the format jk456e (2 letters, 3 nos and one letter). I did not mean the exact ID jk4567 or jk456e, but instead all ID's of this format should be omitted and command should execute on other ID's.

Thanks

Mike
James R. Ferguson
Acclaimed Contributor

Re: Script user list

Hi (again) Mike:

> Thanks (again), I think i presented my question wrongly, what i wanted was, i wanted run the cammonds on all ID's except of the format jk4567 (2 letters, 4 nos) and of the format jk456e (2 letters, 3 nos and one letter). I did not mean the exact ID jk4567 or jk456e, but instead all ID's of this format should be omitted and command should execute on other ID's.

OK, then using the techniques and regular expressions I have shown you, you should be able to do a 'continue' statement in the loop when a 'grep' returns a status (return code) indicating a match for that pattern.

You have the tools :-))

Regards!

...JRF...
MSwift
Regular Advisor

Re: Script user list

JRF:

Thanks, you are truly great, i found the solution (but not w/o your help). Thanks again JRF

Best Regards

Mike
Dennis Handly
Acclaimed Contributor
Solution

Re: Script user list

>if I want to EXCLUDE jk4567 and jk456e,

You can use grep's -v, to exclude patterns.

>JRF: grep ^[a-z][a-z][0-9][0-9][0-9][0-9,a-z]:
>followed by either a digit or a lowercase letter and a ":".

I also see a comma inside []. It could just be a typo. Otherwise it also matches: aa000,:

James R. Ferguson
Acclaimed Contributor

Re: Script user list

Hi (again) Mike:

> Dennis: I also see a comma inside []. It could just be a typo.

Yes, that is indeed a typo and should be eliminated. It was actually posted by Mike as a secondary question of how it worked. I missed that until you saw it :-)

Regards!

...JRF...

MSwift
Regular Advisor

Re: Script user list

Thanks again,

Mike