Create a script which sorts a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a script which sorts a file
# 1  
Old 08-31-2009
Create a script which sorts a file

I have a file below which has a list of users and roles with each row having unique combination of users and roles.

USER1 ROLE1
USER1 ROLE2
USER2
USER3 ROLE1
USER3 ROLE2
USER3 ROLE3
USER4 ROLE2
....
....


I am trying to create a script which sorts the above file to have all the roles in one line delimited by commas per user to look like below:

USER1 ROLE1,ROLE2
USER2
USER3 ROLE1,ROLE2,ROLE3
USER4 ROLE2
....
....


I was able to do this with the script below but when the input is very large it takes very long and takes up cpu usage so I was wondering if someone could tell me a faster way to achive this.


Code:
#!/bin/ksh

INPUT=$1

cut -d' ' -f1 $INPUT | sort -u > USER_LIST

while read USER
do
**** ROLES=$(echo "`grep $USER $INPUT | cut -d' ' -f2 | tr '\12' ','`" | sed -e
's/,$//g')

**** [[ $USER != $ROLES ]] && echo $USER $ROLES
**** [[ $USER = $ROLES ]] && echo $USER

done < USER_LIST


Last edited by Neo; 08-31-2009 at 04:47 AM.. Reason: code tags.
# 2  
Old 08-31-2009
Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums
# 3  
Old 08-31-2009
Something like this :

Code:
awk '{ a[$1]=a[$1]$2"," ;next } END {for ( i in a) {print i" "a[i] }}' File_name |sed 's/,$//'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Poll of sorts - on LDAP

1) Do you use LDAP on AIX? (as a client) 2) If yes, what LDAP server technology do you use: a) IDS (or ITDS) - IBM Tivoli Directory Server b) AD c) openLDAP d) other - please list. I ask, because I am looking at openLDAP as well as IDS and am wondering if there is a clear preference I... (4 Replies)
Discussion started by: MichaelFelt
4 Replies

2. Shell Programming and Scripting

Script to create a CSV file

I created a script that will go out and so a "/sbin/chkconfig --list | egrep XXX" against a server list that would create an output file like the following example: ---------------------------------------------------------------------------------- SERVER1 RC_Script_1 0:off 1:off 2:off... (4 Replies)
Discussion started by: asnatlas
4 Replies

3. Shell Programming and Scripting

Can't create file in CGI Script

Hi I have some simple script within the context of a cgi script that create a file to write to. If I run the script from the command line as root the file is created with no issue. However when I run the script from a web page with the apache account, it dies. The code lines are: sub... (3 Replies)
Discussion started by: larryjmoon
3 Replies

4. Shell Programming and Scripting

Create a file with comment in script

Hello, Be indulgent for my english. Can you help me ? function f1 { } egrep -v '^#' list_file \ | while read arg1 arg2 arg3 arg4; do f1 $arg1 $arg2 $arg3 $arg4 done In list_file there is I want to replace list_file by a $var then when i launch the script with a file's... (13 Replies)
Discussion started by: amazigh42
13 Replies

5. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

6. Shell Programming and Scripting

Script that sorts and show only unique IP

Hi, I have this data: Jun 16 88.191.117.191 = 100 Jun 16 88.191.117.191 = 70 Jun 16 88.191.117.191 = 30 Jun 16 200.242.67.142 = 65 Jun 16 125.77.107.212 = 40 Jun 16 125.77.107.212 = 60 And I want to have the ff ouput: Jun 16 88.191.117.191 = 200 Jun 16 ... (6 Replies)
Discussion started by: linuxgeek
6 Replies

7. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

8. Shell Programming and Scripting

Create a file from within a script

Hi, I want to create a file from within a script that checks whether the file itself exists or not. Like: if ] then echo Creating file... cat << EOF > .theFile echo This line should be in the file EOF fi If I run the script with no condition (commenting... (2 Replies)
Discussion started by: lamano
2 Replies

9. Shell Programming and Scripting

create file in script

hi i am reading a directory, and reading those files one by one i need to write some of file contents to a new file ,for this i need to create a new file for each file please tell me how to create new file in shell script thanks Satya (4 Replies)
Discussion started by: Satyak
4 Replies

10. Shell Programming and Scripting

Create file in script

I am trying to create a new file(string) with todays date in a backup folder string= `date '+%d%m%y'` find * * * /home/SMB-2000/* -print |cpio -ovm > /home/bkups/$string After excecuting I get the messege # ./b2 ... (3 Replies)
Discussion started by: paparazi
3 Replies
Login or Register to Ask a Question