Re: UC Need help in writing a shell/bash script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



I knocked up the enclosed under Cygwin:

#!/bin/sh
(
	cat <<EOTx
                admin                      ankush
                admin                       amit
                powerusers               dinesh
                powerusers               jitendra
EOTx
) | awk '
	{
		grpnm[$1] = grpnm[$1] ", " $2
	}
	END {
		for (i in grpnm) {
			print i ":	" substr(grpnm[i], 3)
		}
	}
' | sort

The meat is the AWK programme.  If collects all instances of the second
column in an array indexed on the entries in the first column.  At the
end of the input file it handles each element of the array in turn,
dropping the grammatically incorrect leading comma and space.  The sort
just sorts lines alphabetically, as you implied.  The ( cat ... ) |
construct is just to push in your test data.

Are the headings part of the file?  In which case you may need to add a
line:

	NR == 1 { next }

immediately after the awk line.

HTH,

Martin Rushton
HPC System Manager, Weapons Technologies
Tel: 01959 514777, Mobile: 07939 219057
email: jmrushton@xxxxxxxxxxx
www.QinetiQ.com
QinetiQ - Delivering customer-focused solutions

Please consider the environment before printing this email.
-----Original Message-----
From: centos-bounces@xxxxxxxxxx [mailto:centos-bounces@xxxxxxxxxx] On
Behalf Of ankush grover
Sent: 30 December 2011 12:01
To: CentOS mailing list
Subject:  Need help in writing a shell/bash script

Hi Friends,

I am trying to write a shell script which can merge the 2 columns into
3rd one on Centos 5. The file is very long around 31200 rows having
around 1370 unique groups and around 12000 unique user-names.
The 1st column is the groupname and then 2nd column is the user-name.

1st Column (Groupname)            2nd Column (username)
                admin                      ankush
                admin                       amit
                powerusers               dinesh
                powerusers               jitendra




The desired output should be like this

admin:   ankush, amit
powerusers:  dinesh, jitendra


There are commands available but not able to use it properly to get the
desired output. Please help me



Thanks & Regards

Ankush
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos
This email and any attachments to it may be confidential and are
intended solely for the use of the individual to whom it is 
addressed. If you are not the intended recipient of this email,
you must neither take any action based upon its contents, nor 
copy or show it to anyone. Please contact the sender if you 
believe you have received this email in error. QinetiQ may 
monitor email traffic data and also the content of email for 
the purposes of security. QinetiQ Limited (Registered in England
& Wales: Company Number: 3796233) Registered office: Cody Technology 
Park, Ively Road, Farnborough, Hampshire, GU14 0LX  http://www.qinetiq.com.
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux