1835086 Members
2197 Online
110073 Solutions
New Discussion

Re: Scripting Question

 
Andrew Beal
Frequent Advisor

Scripting Question

Hi, I have a list of names and I need to check to see if any of them exist on the unix system. There are like 200 of them, and I dont feel like using grep for each one! lol I tried writing a script to grep the names out of the passwd file, but I am not having much luck. I would appreciate it if someone could point me in the right direction as to which commands I should use, or has already got a script to perfom a similar task.

the names in the list are written in the following format.

joe bloggs
harry potter
incredible hulk
space cow

but there are like 200 of them...

Thanks in advance,

Andrew
9 REPLIES 9
Jordan Bean
Honored Contributor

Re: Scripting Question

This might get some of them...

grep -if - /etc/passwd < namelist.txt

Michael Tully
Honored Contributor

Re: Scripting Question

hiccup ... hiccup ...
Anyone for a Mutiny ?
Paula J Frazer-Campbell
Honored Contributor

Re: Scripting Question

Hi

Try this :-

cat /etc/passwd | sed 's/:/ /' | awk '{print $1}' | sed 's/^/ /' | sed 's/ /last /' >/tmp/name check

then diff the /tmp/namecheck against your name list.


You also might find this useful :-

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x37857d4cf554d611abdb0090277a778c,00.html


HTH

Paula
If you can spell SysAdmin then you is one - anon
john korterman
Honored Contributor

Re: Scripting Question

Hi Andrew,
you could try something like this:

#!/usr/bin/sh
while read firstname lastname
do
grep -iE "${firstname}[[:space:]]*${lastname}" /etc/passwd
done <$1


run it like this:
# myscript.sh namefile

handles multiple spaces between first and second names in /etc/passwd. Names in namefile consisting of other than two names will also be processed, however literally.
If the content of your namefile causes an entry to be output more than once, then execute like this:
# myscript.sh namefile | sort -u

regards,
John K.
it would be nice if you always got a second chance
Jordan Bean
Honored Contributor

Re: Scripting Question

Oh they did go through! What's up with that? When I clicked submit, nothing happened... probably the browser. Sorry!
Chris Vail
Honored Contributor

Re: Scripting Question

I'd just do something like:
for NAME in `cat $MYFILE`
do
grep $NAME /etc/passwd
if test "$?" -eq "0"
then
echo $NAME>>FOUNDFILE
else
echo $NAME>>NOTFOUNDFILE
fi
done

This would give you two files, one with the names of the users that were in the password table, and another with the names that weren't.


Chris
Dario_1
Trusted Contributor

Re: Scripting Question

Hi Andrew:

Try this:

for NAME in `cat filewnames`
do
grep -x $NAME /etc/passwd >> output.txt
done

Make sure you create the output.txt file before executing the for i statement.

Regards,

DR
Leif Halvarsson_2
Honored Contributor

Re: Scripting Question

Hi,
Have a look at the join command (man join) , it matches fields from two files .
The files must be sorted on the matching field (you have to create a temporary file for the sorted password file).

Leif Halvarsson_2
Honored Contributor

Re: Scripting Question

Hi,
Simple example of the join command:
sort -t : -k 5 /etc/passwd >tmp1
sort list >tmp2
join -t : -j1 1 -j2 5 -o 1.1 tmp2 tmp1

Prints out the name from list for each matching line in passwd.