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!! :frowning:

Support data is

$ 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

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)
        }
        printf("\n")
}' b.txt

And the desired output is:

$ ./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

Don't know why I cannot edit my own post. Anyway, 3 redundant lines left over from old work. Here is the cleaner version

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)
        }
        printf("\n")
}' b.txt

With Perl:

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