formatting script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formatting script
# 1  
Old 04-27-2009
formatting script

Disclaimer: I did not set out to write the next big thing...

I'm simply collecting all of the group information for all of the users across disparate hosts.

I wrote a simple script to collect all of the group information for each user on a system and pushed it out to each host:
Code:
#!/bin/sh
for user in `awk -F: '{print $1}' /etc/passwd`; do echo "$user:"; groups $user; echo "#################"; done

I collected them on a central host
Code:
for host in `cat hosts`; do ssh $host /tmp/myScript.sh >> ../collected/$host; done

To clean it up and move each new group onto it's own line, I quickly ran it through tr
Code:
cd ../collected
for file in `ls`; do tr ' ' '\n' < $file >> ../uidgames/$file; done

But now, I'm left with a handful of files that look like this:
Code:
daemon:
other
bin
adm
daemon
#################
bin:
bin
sys
#################
sys:
sys
#################
adm:
adm
sys
tty
lp
#################
lp:
lp
#################

Ideally, I'd like the file to look like this:
Code:
daemon:other
daemon:bin
daemon:adm
daemon:daemon
bin:bin
bin:sys
sys:sys
adm:adm
adm:sys
adm:tty
adm:lp
lp:lp

And then I'll collect them all together with a simple:
Code:
for file in `ls`; do awk '{print FILENAME":"$0}' $file >> ../result; done

This is a file that I can drop into Excel to get moving on my UID/GID cleanup plan.

I've been playing with this for 30 minutes, but need to go home before I start seeing hash marks everywhere.

If you have a great idea to do this all from go to whoa, great, but all I'm really looking for is that one piece. I'll munge it all together later, just looking for "quick and dirty" right now...

Thanks!

- Avron
# 2  
Old 04-27-2009
Is this what you are looking for ?

Code:
for user in `awk -F: '{print $1}' /etc/passwd`; do groups $user|awk '{n=split($0,x); for (i=3; i<=n; ++i) {print x[1]":"x[i];}}'; done

Code:
$                                                                                                             
$ for user in `awk -F: '{print $1}' /etc/passwd`; do groups $user|awk '{n=split($0,x); for (i=3; i<=n; ++i) {print x[1]":"x[i];}}'; done
root:root
root:bin 
root:daemon
root:sys   
root:adm   
root:disk  
root:wheel 
bin:bin    
bin:daemon 
bin:sys    
daemon:daemon
daemon:bin   
daemon:adm   
daemon:lp    
adm:adm      
adm:sys      
lp:lp        
sync:root
shutdown:root
halt:root
mail:mail
uucp:uucp
operator:root
games:users
...
... (clipped output)

HTH,
tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."
# 3  
Old 04-28-2009
Looks great, thanks!

I like how you trimmed out two complete portions of my hackery with the 'awk split'. Back to the books for me, it seems...

Cheers,

- Avron
# 4  
Old 04-28-2009
For the record book...

The solaris output from the groups command is different from the linux output:

linux:
Code:
[root@hostname ~]# groups root
root : root bin daemon sys adm disk wheel

Solaris:
Code:
[root@hostname ~]# groups root
root other bin sys adm uucp mail tty lp nuucp daemon

A simple change of i=3 to i=1, and "Bob's your uncle"!

Thanks again,

- Avron
# 5  
Old 04-28-2009
blast - it's not so simple as that, either... I'm still digging at this one...
# 6  
Old 04-28-2009
Quote:
Originally Posted by avronius
...
A simple change of i=3 to i=1, ...
...
And maybe x[1] to $user as well ?

(Sorry, no Solaris around here - only windblows.)

tyler_durden

______________________________________________
"Only after disaster can we be resurrected."
# 7  
Old 04-28-2009
Yeah, no.

I'm getting multiple lines for users that are actually groups. I'll approach this from another angle.

I've slightly modified my original scripts to do the following:

1. Run this script on each of the unique hosts
Code:
#!/bin/sh
for user in `awk -F: '{print $1}' /etc/passwd`; do printf "$user:" ; groups $user ; done

The above script provides the results below:
Code:
root:root other bin sys adm uucp mail tty lp nuucp daemon
daemon:other bin adm
bin:bin sys
sys:sys
adm:adm sys tty lp
lp:lp
uucp:uucp
nuucp:nuucp
smmsp:smmsp
listen:adm
gdm:gdm
webservd:webservd
postgres:postgres
svctag:daemon
nobody:nobody
noaccess:noaccess
nobody4:nogroup

2. Collect the data with this script to an appropriate directory:
Code:
for host in `cat hosts`; do ssh $host /tmp/myScript.sh >> $host; done

3. Run a script to clean it up with results in the format of:
$host:username:group
$host:username:group2
...

I've tried mangling the script provided by Tyler, but it just doesn't work reliably. I will throw together a quick perl script to break it into a hash with array members. It's not as smooth as I'd like, but it will get me through this problem. I'll post the .pl here after lunch.

- Avron
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need some help in formatting answer of shell script

Dear all, I am new to shell scripting and have tried to make a small program for fun sake that tells age from the last number of cell phone and birth year. At the end we get a final 3 digit number and the first digit represents the last digit of cell phone and rest 2 numbers shows age. eg. 423... (4 Replies)
Discussion started by: stg44
4 Replies

2. Shell Programming and Scripting

Formatting output of script

I'm looking for a way to neaten up the out put of an ldapquery. I might get one, none, or several returns for each query; and each query consists of multiple lines. And while each return will contain the same types of info, it might be in a different order, like: uniq: 1 ip: 192.168.2.3... (3 Replies)
Discussion started by: jnojr
3 Replies

3. Shell Programming and Scripting

formatting a shell script

there is one shell which calls for different sql scripts. output of different are formatted in frame like structure.i want columnar wise formatting to be done in such a manner that output of one sql may be comon input of rest sqls . ERC DML Closure Date ER DML SAP Transfer Date ... (0 Replies)
Discussion started by: arc
0 Replies

4. Shell Programming and Scripting

Script formatting

Hi, ls -ltre total 4 drwxr-xr-x 9 wlsuser wlsgrp 1024 Feb 14 12:23:14 2011 _WL_internal drwxr-xr-x 3 wlsuser wlsgrp 96 Mar 8 18:11:33 2011 _WL_user -rw-r--r-- 1 wlsuser wlsgrp 48 May 23 10:35:30 2011 WebServiceUtils.ser -rw-r--r-- 1 wlsuser... (7 Replies)
Discussion started by: mohtashims
7 Replies

5. Shell Programming and Scripting

Help needed in formatting script files

Hi, Can anyone tell me how i can convert all tab spaces inside a script to 4 spaces through another script. Also i need to find if all the quotes are matching and ended properly. Any idea whould be of great help. Many thanks! (3 Replies)
Discussion started by: justchill
3 Replies

6. Shell Programming and Scripting

Formatting the output of a script

Dear readers, I have a script that counts the number of files in particular directories in my home location and displays the output. Now I have 13 directories in my home location. I am getting the output as : Avishek_dir 13 Kunal_dir 17 Shantanu_dir 18 Arup_dir 12 Pranabesh_dir 19 . .... (7 Replies)
Discussion started by: avishek007
7 Replies

7. Shell Programming and Scripting

Script for data formatting

Hi I have to convert the data in a file ******* 01-20-09 11:14AM 60928 ABC Valuation-2009.xls 01-20-09 11:16AM 55808 DEF GHI Equation-2009.xls 01-20-09 11:02AM 52736 ABC DF Valuation-2009.xls 01-20-09 11:06AM 89600 THE... (6 Replies)
Discussion started by: shekhar_v4
6 Replies

8. Shell Programming and Scripting

awk script formatting

Hello, I have got the following kine in my script awk '{printf("%s,", $0);next}{printf("%s", $0)}' ORS="," a.txt > b.out The contents of b looks somewaht like this: QUEUE(QUEUE1.Q),CURDEPTH(0),QUEUE(QUEUE2.Q),CURDEPTH(0),QUEUE(QUEUE3.Q),CURDEPTH(0) But my desired output is :... (10 Replies)
Discussion started by: King Nothing
10 Replies

9. Shell Programming and Scripting

Formatting Script Output

Hello all, Can somebody please offer some advice. I'm working on a small script to list all packages on the local and remote systems and need it in the following format. Machine Name, Package, Version Here is what i have so far but i can't seem to get the output of the hostname to loop to... (3 Replies)
Discussion started by: liketheshell
3 Replies

10. UNIX for Advanced & Expert Users

Date Formatting in Shell Script

Hi, Can anybody throw somelight on how to handle date in a shell script? I need to pass the date in this format 'yymmdd' and then pad it to a file. The way in which i've handled is given below, but the date is not getting passed rather the file is getting created with a null date. Any... (2 Replies)
Discussion started by: Isiva
2 Replies
Login or Register to Ask a Question