1833875 Members
3113 Online
110063 Solutions
New Discussion

Re: Shell Scripting Help

 
David Peacock
Frequent Advisor

Shell Scripting Help

I want to make a script that will output
UID User Name and Information.
In the script I wrote the collumns do not
line up. Can you help me align the collumns?

#!/bin/ksh
cat /etc/passwd > /tmp/build0
sort -t: +2 /tmp/build0 > /tmp/build1

grep -v root /tmp/build1 > /tmp/build2
grep -v datatel /tmp/build2 > /tmp/build3
grep -v wordmarc /tmp/build3 > /tmp/build4
grep -v artsmart /tmp/build4 > /tmp/build5
grep -v udmsmgr /tmp/build5 > /tmp/build6
grep -v :bin: /tmp/build6 > /tmp/build7
grep -v lp /tmp/build7 > /tmp/build8
grep -v uucp /tmp/build8 > /tmp/build9
grep -v www /tmp/build9 > /tmp/build10
grep -v nobody /tmp/build10 > /tmp/build11
grep -v adm /tmp/build11 > /tmp/build12
grep -v sys /tmp/build12 > /tmp/build13
grep -v hpdb /tmp/build13 > /tmp/build14

awk -F: '{print $3, $5}' /tmp/build14 > /tmp/build15

sed 's/,/ /g' /tmp/build15 > /tmp/build16
veni, vidi, vmstat
8 REPLIES 8
David Peacock
Frequent Advisor

Re: Shell Scripting Help

This line:
sed 's/,/ /g' /tmp/build15 > /tmp/build16

Should read:

sed 's/,/ /g' /tmp/build15 > /tmp/build16

veni, vidi, vmstat
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Scripting Help

Hi David:

First, I would replace all those temp files with a series of pipes. Simply pipe the output of 1 command to the next. To fix your alignment change your awk print statement to a printf.

e.g.
awk -F: '{printf("%5d\t%s\n",$3,$5)}' /tmp/build14 > /tmp/build15

That should do it, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Scripting Help

Hi again,

If you use the printf to format the output, you shouldn't even need your last sed. I did intentionally insert a tab \t in the printf but, if you prefer, you can simply replace it with a space.
If it ain't broke, I can fix that.
SHABU KHAN
Trusted Contributor

Re: Shell Scripting Help

You could do this:

awk -F: '{print $3,$1,$5}' /etc/passwd | egrep -v 'root|nobody' > /tmp/password_format.out

Add the strings that you would want to get rid of after nobody and as many pipes as you would need ...

Hope this helps !

-Shabu
RAJESH GANGADHARAN
Regular Advisor

Re: Shell Scripting Help

It is better to use "printf" insted of "print" along with awk.
Let the choices you make today be the choices you can live with tomorrow.
Kirk Gardner
Advisor

Re: Shell Scripting Help

There is a HP-UX command that may be of interest here:

# logins -u
harry d brown jr
Honored Contributor

Re: Shell Scripting Help


cat /etc/passwd | sort -t: +2 | grep -v -e root -e datatel -e wordmarc -e artsma
rt -e udmsmgr -e ":bin:" -e lp -e uucp -e www -e nobody -e adm -e sys -e hpdb |
cut -d":" -f3,5 | sed 's/,/ /g'| awk -F: '{printf("%5d %s\n",$1,$2)}'


Note with grep, you can use the "-e" option for more pattern matches.
I also removed all of the unnecessary temporary files, and I used awk to format the output.

live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: Shell Scripting Help

Hi David,

Another thing you may want to consider is using the -f switch in grep. Put all the strings to exclude in a file and use:

cat /etc/passwd | sort -t: +2 | grep -v -f exclude_file | cut -d":" -f3,5 | sed 's/,/ /g'| awk -F: '{printf("%5d %s\n",$1,$2)}'

This might make it easier to maintain should you wish to amend the list.

Rgds, Robin.