Register
It is currently Sat Nov 01, 2014 6:37 am

Get RANDOM person from the group


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Apr 05, 2014 2:35 am   

Joined: Sat Apr 05, 2014 2:25 am
Posts: 2
Hi guys, i've got a problem.
I need to get a random person name from my group. I can get all of them and print the list, but i didn't find the way how to print one name randomly.
This is my start:

cat /etc/group | grep :`cat /etc/passwd | grep :$UID: | cut -d: -f4`: | cut -d: -f4

output
katerina.bidrmanova,eva.brynychova,tomas.feigl,marcela.homutova,robin.horacek,zdenka.hoskova,frantisek.kadlec,martin.knob,lucie.konvalinkova,lubos.krkoska,vera.kucerova,jiri.machulka,jindrich.maruska,katerina.maranova,pavla.mikezova,martin.parizek,lucie.pickova,tomas.piller,vlastimil.piller,milan.pisna,karel.prokop,jiri.rezler,ivan.schwarz,jiri.kurt.strohschneider,erika.starmanova,jan.trisko,martin.vanatko,josef.vosvrda,vera.vyslouzilova,chip,dale,monty,gadgetka,zip

now i need only one RANDOM name from the list.

THANK YOU!


Top
 Profile  
 PostPosted: Sat Apr 05, 2014 3:45 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
hi,

avoid uuoc, and use an array:
Code:
IFS=',' read -a array < <(grep ":$(grep ":$UID:" /etc/passwd | cut -d: -f4):" /etc/group | cut -d: -f4)
echo "${array[RANDOM%${#array[@]}]}"
I'd use awk instead of grep|cut; I'd even try to use a single awk statement parsing the two files


Top
 Profile  
 PostPosted: Sat Apr 05, 2014 12:56 pm   

Joined: Sat Apr 05, 2014 2:25 am
Posts: 2
AWESOME!

Thank you a lot! You're my lifesaver! :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP