1834020 Members
2782 Online
110063 Solutions
New Discussion

Script needed here

 
SOLVED
Go to solution
E. Wong
Frequent Advisor

Script needed here

I want to create a script that will manipulate /etc/passwd and create a new file with the following:

- Filter to list all logins that have default directory starting with /home/*

- Show only two columns, the userID, and Real Name.

- The order must be Real Name, a comma (,) and userID.

So, far I have

cat /etc/passwd | grep /home>>file1
cat file1 | cut -f 1,5 -d:>file2
rm file1

After this, I am not sure how to invert the order of the columns, so it will be real name first and then userID

Any help appreciated. With points of course!
compute, therefore you are
9 REPLIES 9
Vitek Pepas
Valued Contributor
Solution

Re: Script needed here

IFS=:
while read uid realid
do
echo "$realid,$uid"
done file3
Steven E. Protter
Exalted Contributor

Re: Script needed here

Original:

cat /etc/passwd | grep /home>>file1
cat file1 | cut -f 1,5 -d:>file2
rm file1

My approach would be to modify the script I'm attaching to put your columns out with awk.

You can use awk to print certain columns any way you want.

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
Mark Grant
Honored Contributor

Re: Script needed here

How about awk for this one.

something like

awk -F: '$6 ~ "/home" {
printf "%s,%s\n",$5,$3
}' /etc/passwd > newfile
Never preceed any demonstration with anything more predictive than "watch this"
Volker Borowski
Honored Contributor

Re: Script needed here

Like this :

grep /home /etc/passwd | cut -d: -f 1,5 | awk -F':' '{ print $2 "," $1 }'

Consider to print qoutes around $2, in case the real name follows the convention
"Name , Location, Phone"
which will give your "comma" a bad standing to be a good delimiter.

Hope this helps
Volker
Vitek Pepas
Valued Contributor

Re: Script needed here

better yet:

#!/bin/ksh
IFS=:
while read aa bb cc dd ee ff gg
do
[[ $ff = /home/* ]] && echo "$ee,$aa"
done < /etc/passwd > file3
Mark Grant
Honored Contributor

Re: Script needed here

It's amazing how everybody comes up with completely different aproaches to this all within about five minutes!

Have to say I think Vitak beats my awk mangling :)
Never preceed any demonstration with anything more predictive than "watch this"
Volker Borowski
Honored Contributor

Re: Script needed here

Correct Mark,

I'll bet, scripting-questions are the ones that get the fastest solutions all over the forum.
Guess because nearly everybody has to do with it, so the number of forumers who can provide a bit of knowledge is a whole lot broader than those able to tell something about a specific raid-controller.

And of course: solving puzzles is always fun.

Have fun
Volker
A. Clay Stephenson
Acclaimed Contributor

Re: Script needed here

I'm sure that most people will give a shell script example but the attached Perl script will be cheaper, faster, better AND will work with trusted systems, untrusted (plain old /etc/passwd) systems, and systems running NIS/NIS+ all without changes. I assume that by "Real Name" you really mean the gecos field.

If it ain't broke, I can fix that.
E. Wong
Frequent Advisor

Re: Script needed here

Wow, just got back from lunch and got all these good scripts examples!

I've tried them all, and they all work for the same results. After reviewing them all, I will have to give points to everyone.

Vitek, very good script! I am really pleased with it.

Volker your script is swift. Just one line, I will use your script after reviewing it with my staff (unanimous decision). Please reply for additional bonus points.

Thanks everyone who responded for the scripts, suggestions, and help.

E.
compute, therefore you are