sort second columns in file.

File will have two columns key column and second column which is pipe separated and that need to be sorted.

Below is input file.

 
1, D|B|A|C 
2, C|A|B
3, E|A|F|G|H|D|B|C
4, A|B|D|C|F
 

Output should be

 
1, A|B|C|D 
2, A|B|C
3, A|B|C|D|E|F|G|H
4, A|B|D|C|F
 

Thanks in advance and I appreciate your help

File will have two columns key column and second column which is pipe separated and that need to be sorted.

Below is input file.

 
1, D|B|A|C 
2, C|A|B
3, E|A|F|G|H|D|B|C
4, A|B|D|C|F

Output should be

 
1, A|B|C|D 
2, A|B|C
3, A|B|C|D|E|F|G|H
4, A|B|D|C|F

Thanks in advance and I appreciate your help.

Stop crossposting please.

---------- Post updated at 09:45 AM ---------- Previous update was at 09:07 AM ----------

awk '{ split($2, A, "|"); asort(A); $2=""; for(N=1; A[N]; N++) $2=$2"|"A[N]; print $1 " " substr($2, 2) }' filename

Try:

perl -ane 'print "$F[0] ";$,="|";print sort split /\|/, $F[1];print "\n"' file

sorry i did not mention early, I can't use perl on my system.
It has to be awk or sed .

again sorry for not clearing that up before and thanks for solution.

Top of my head job, but something like the following

perl -e 'while(<>){chomp;
@record=split(/\s+/,$_);
@fields=split(/|/,$record[1]);
@sorted_fields=sort(@fields);
print "$record[0] " .join("|",@sorted_fields)."\n";}' tmp.dat

I posted in your 'other' thread and lost the roulette wheel. I'll repost in this one in the hope that when mods close one of your crossposts, they'll close the shorter one.

awk '{ split($2, A, "|");
        asort(A);
        $2="";
        for(N=1; A[N]; N++) $2=$2"|"A[N];
        print $1 " " substr($2, 2) }' filename

FWIW - solaris awks and nawk do not support the asort() function. That is a gawk thing (GNU awk). You can install gawk from

Blastwave.org - An OpenSolaris Community Site

$ perl -F"," -lane 'chomp($F[1]);$F[1]=~s/^ //;@x=sort (split/\|/,$F[1]);print "$F[0], ",join("\|",@x)' inputfile
1, A|B|C|D
2, A|B|C
3, A|B|C|D|E|F|G|H
4, A|B|C|D|F

Yes you are right this will not work on solaris

awk '{ split($2, A, "|");asort(A);$2="";for(N=1; A[N]; N++) $2=$2"|"A[N];print $1 " " substr($2, 2) }' test123.dat

awk: illegal reference to array A
record number 1

cross-posted thread thread merged.

sorry for the repost as this is my first post.
Perl solution seems to work but i can not use due to limitation on my server.