1828663 Members
1543 Online
109984 Solutions
New Discussion

Re: scripting user list

 
SOLVED
Go to solution
MSwift
Regular Advisor

scripting user list

Here is what i trying to do. First, i am trying to generate a userlist file (where i want to (logins -u) and capture only the id's which are names (we have most id's which are in the format of (2 alphabets and 4 numbers) (ex, jk4567) and we want to exclude those from the userlist file, so that we have the other id's. once the userlist file is generated, then we want to read that file and run a command $USERID so that it modifies the attributes for each id (which are NOT in jk4567) format. Could someone help with this Please?

Thanks

Mike
19 REPLIES 19
Dennis Handly
Acclaimed Contributor

Re: scripting user list

Just use grep:
grep -v '^[a-zA-Z]\{2\}[0-9]\{4\}' user-list
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi Mike:

One way to generate your file of ids is this way:

# perl -e 'open($fh,"-|","logins -u") or die;while (<$fh>) {@F=split;print unless $F[0]=~m{\d+}}' > /var/tmp/logins

Then you can do:

#!/usr/bin/sh
while read USERID X
do
echo ${USERID}
done < /var/tmp/logins

You could imbed the Perl snippet in your shell script too.

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF:

How would put all this in the same shell? Also is there a way the whole thing could be done via a shell script alone?

Thanks

Mike
Dennis Handly
Acclaimed Contributor

Re: scripting user list

>is there a way the whole thing could be done via a shell script alone?

You don't want to use my grep or JRF's perl solution?

You can also use a real shell's pattern matching:
if [[ name != [a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9] ]]; then
Dennis Handly
Acclaimed Contributor

Re: scripting user list

Oops, that should have been:
if [[ "$name" != [a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9] ]]; then
James R. Ferguson
Acclaimed Contributor
Solution

Re: scripting user list

Hi (again) Mike:

> How would put all this in the same shell? Also is there a way the whole thing could be done via a shell script alone?

#!/usr/bin/sh
#!/usr/bin/sh
set -u
MYFILE=/var/tmp/mylogins
trap 'rm ${MYFILE}' EXIT
perl -e 'open($fh,"-|","logins -u") or die;
while (<$fh>) {@F=split;print unless $F[0]=~m{\d+}}' > ${MYFILE}
while read USERID X
do
echo ${USERID}
done < ${MYFILE}

...in pure Perl, I might do:

#!/usr/bin/perl
use strict;
use warnings;
my @F;
my $file = '/var/tmp/mylogins';
open(my $fh, "-|", "logins -u") or die "Can't open $file:$!\n";
while (<$fh>) {
@F=split;
system( "echo", $F[0] ) unless $F[0]=~m{\d+};
}
1;

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi Mike:

Here's a pure shell variation along the lines that Dennis began:

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

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF:

One more question. in the script above i see 2 echo statements, i plug in the actual command instead of the second echo statement- correct?

Thanks

Mike.
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi (again) Mike:

> One more question. in the script above i see 2 echo statements, i plug in the actual command instead of the second echo statement- correct?

Yes.

Given:

# echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && echo ${USERID}

...we are feeding the ${USERID} value to 'grep' with the first 'echo'. If the return value from 'grep' is zero, there was a match and the second statement (another 'echo')is run. Hence, this is the one where you want to substitute the real command.

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF:

GM,

The script worked like a charm for all ID's except one id..the id name was dba10gaf, not sure why the script( shell version above) did not work for this ID.

Thanks

Mike
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi (again) Mike:

> The script worked like a charm for all ID's except one id..the id name was dba10gaf, not sure why the script( shell version above) did not work for this ID.

If you dropped the caret (beginning-of-line) anchor, you would see this behavior:

# USERID=dba10gaf
# echo ${USERID}|grep -q -E '^[a-zA-Z]+$' && echo ${USERID}
#
# echo ${USERID}|grep -q -E '[a-zA-Z]+$' && echo ${USERID}
dba10gaf

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: scripting user list

Shalom,

The grep statement is not picking up numbers in the id.

echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && echo ${USERID}

Changes to...
echo ${USERID} | grep -q -E '^[a-zA-Z0-9]+$' && echo ${USERID}

Untested.

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
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi:

> SEP: The grep statement is not picking up numbers in the id.

SEP, _READ_ the initial OP request again. Mike wants to EXCLUDE accounts like 'jk4567'.

...JRF...
MSwift
Regular Advisor

Re: scripting user list

so JRF,

do you recommenend to use..

echo ${USERID}|grep -q -E '[a-zA-Z]+$' && echo ${USERID}
?

Thanks

MIke
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi (again) Mike:

> do you recommenend to use..

> echo ${USERID}|grep -q -E '[a-zA-Z]+$' && echo ${USERID}

No, while this will eliminate accounts like 'jk4567' it will not eliminate accounts like 'jk45lm'.

# echo ${USERID} | grep -q -E '^[a-zA-Z]+$' && echo ${USERID}

...skips both the aforementioned accounts. Isn't that what you want?

If so, notice the caret (^) to anchor the match to the beginning of the string, together with the dollar-sign ($) to anchor to the end of the string.

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF

You are correct about excluding jk4567. only these needs to be excluded.(2numbers4letters exact(like 2 numbers first, followed by four letters- total 6) but i still need id's like jk45lm or longer id's (than 6)dba10gaf etc. Thanks JRF, once again

Thanks

Mike
James R. Ferguson
Acclaimed Contributor

Re: scripting user list

Hi Mike:

> You are correct about excluding jk4567. only these needs to be excluded.(2numbers4letters exact(like 2 numbers first, followed by four letters- total 6) but i still need id's like jk45lm or longer id's (than 6)dba10gaf etc.

See if this meets your needs:

# echo ${USERID} | grep -q -E '^[a-zA-Z]+[0-9]+[a-zA-Z]+$' && echo ${USERID}

Regards!

...JRF...
MSwift
Regular Advisor

Re: scripting user list

JRF

THANKS, it works

Mike
Dennis Handly
Acclaimed Contributor

Re: scripting user list

>(2numbers4letters exact (like 2 numbers first, followed by four letters- total 6)

If you wanted exactly this, you could have used my regexp.