Help sorting .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help sorting .csv file
# 1  
Old 11-14-2008
Help sorting .csv file

Hi,

I have a .csv file which contains script names, subjects and email_addresses
The first two colums are always script name and subject, the next 20 colums are email address..

What i want to do is sort the email address in alphabetical order for each row and there's around 1200 rows.

So instead of it looking like script1,subject1,j@p.com,a@p.com,c@hi.com
it'll look like
script1,subject1,a@p.com,c@hi.com,j@p.com


Or if anyone could tell me how to extract one instance of every email found in this .csv file and dump them into a new file that would be nice too


How would i go about doing this?

Using .ksh by the way.

Please help a lost and desparate person!! Smilie

Last edited by Jazmania; 11-14-2008 at 09:25 AM..
# 2  
Old 11-14-2008
Support data is
Code:
$ cat b.txt
script1,subject1,j@p.com,a@p.com,c@hi.com
script2,subject2,J@p.com,A@p.com,C@hi.com
script3,subject2,Jj@p.com,Aa@p.com,Cc@hi.com

Here is a gawk script. If you are running it on Solaris, asort is not available in their awk/nawk. You need to write your own sort function. Just google 'awk sort function', they will show you one
Code:
awk '
BEGIN {
        FS=","
        pos=1
        email[1]=1
        sorted[2]=2
}
{
        delete email
        delete sorted
        script=$1
        subject=$2
        for(i=3;i<=NF;++i) {
                email[i-2]=$i
        }
        asort(email,sorted)

        printf("%s,%s",script,subject)
        for(i=1;i<=NF-2;++i) {
                printf(",%s",sorted[i])
        }
        printf("\n")
}' b.txt

And the desired output is:
Code:
$ ./b.sh
script1,subject1,a@p.com,c@hi.com,j@p.com
script2,subject2,A@p.com,C@hi.com,J@p.com
script3,subject2,Aa@p.com,Cc@hi.com,Jj@p.com

# 3  
Old 11-14-2008
Don't know why I cannot edit my own post. Anyway, 3 redundant lines left over from old work. Here is the cleaner version
Code:
awk '
BEGIN {
        FS=","
}
{
        delete email
        delete sorted
        script=$1
        subject=$2
        for(i=3;i<=NF;++i) {
                email[i-2]=$i
        }
        asort(email,sorted)

        printf("%s,%s",script,subject)
        for(i=1;i<=NF-2;++i) {
                printf(",%s",sorted[i])
        }
        printf("\n")
}' b.txt

# 4  
Old 11-14-2008
With Perl:

Code:
perl -F, -lape'
  $_ = join ",", @F[0,1], sort @F[2..$#F]
  ' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Sorting a CSV file by DOB

I have absolutaly no idea how to get this script to sort the info in Birthdays.csv by date of birth. I know the sort -n command, however i wish to sort the file birthdays.csv by DOB. How would i go about doing this? The below script gets user info and date of birth and then puts these info a... (3 Replies)
Discussion started by: redshine6
3 Replies

4. Shell Programming and Scripting

Sorting the csv file in Perl

Hi All How all are doing today. Just struck in an issue in Perl I have a csv file which contain 32 column, I want to make sorting in that csv file with respect to 26th column. Is it possible to do so without any module being added? Regards Aditya ---------- Post updated at 10:02 AM... (11 Replies)
Discussion started by: adisky123
11 Replies

5. Shell Programming and Scripting

CSV Sorting on only particular columns

Hello! So ive been presented with this comma-delimited file: I need a print to look as below " lastname, phone_number, zip for every person with a last name starting with the letter H, I only with a 650-area code phone number. output should be sorted by reverse ZIP code " I only have... (5 Replies)
Discussion started by: strangemachine
5 Replies

6. Shell Programming and Scripting

Sorting a .csv using awk or other

Hello all, I am new here and *relatively* new to Unix. I have a bit of an emergency. I have a three column file that I need to sort: sample name, miRNA, reads per million (RPM) There are multiple samples, and for each sample name there are multiple miRNAs and associated RPMs. Some of these... (6 Replies)
Discussion started by: dunnybocter
6 Replies

7. Shell Programming and Scripting

Need help in writing a routine for sorting a CSV file

Hi, I have a CSV file in following manner: server1,env1,patch1 server1,env1,patch2 server1,env1,patch3 server1,env2,patch1 server1,env2,patch3 server2,env3,patch1 server2,env3,patch5 server2,env4,patch1 server3,env6,patch1 server3,env7,patch2 server3,env7,patch3 I want to... (6 Replies)
Discussion started by: avikaljain
6 Replies

8. UNIX and Linux Applications

UNIX sorting - csv file

Hi, Please help me to solve sorting in CSV file. I have 25 columns in my CSV. (Delimiter is ",") Summary columns are from 10 to 13 which are should not be sorted. From 1-9 and 14-25 should be able to sort in ascending. how should i do this using Sort command in unix. If no simple commands pls... (0 Replies)
Discussion started by: rajani_p
0 Replies

9. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question