sort each column of text file alone

Hello ,

i have a text file like this

1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW 
3 a3 ;AS ,UY ;RF ,YT

i want to sort this text file based on each row , and excluding 2nd column from the sorting and not taking the comma or ; into consideration in the sorting, so it will become like this :

1 a1 ,AB ,AC ;AD ,EE 
2 a2 ;TR ,WE ,WW ,YT  
3 a3 ;AS ;RF ,UY ,YT 

is this can be done ?
thanks in advance

Try:

perl -nae '@F[2..$#F]=sort {$x=$a;$y=$b;$x=~s/[,;]//;$y=~s/[,;]//;$x cmp $y} @F[2..$#F];print "@F\n"' file

Hello , thanks for your reply , but it did not worked

What output did you get? It worked for me...

[root@linux ~]# cat file
1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW
3 a3 ;AS ,UY ;RF ,YT
[root@linux ~]#
[root@linux ~]# perl -nae '@F[2..$#F]=sort {$x=$a;$y=$b;$x=~s/[,;]//;$y=~s/[,;]//;$x cmp $y} @F[2..$#F];print "@F\n"' file
1 a1 ,AB ,AC ;AD ,EE
2 a2 ;TR ,WE ,WW ,YT
3 a3 ;AS ;RF ,UY ,YT

try:

echo '1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW
3 a3 ;AS ,UY ;RF ,YT'  |awk '{printf $1 FS $2 FS;for(i=3;i<=NF;i++) b=$i;l=asort(b); for(j=1;j<=l;j++) print b[j] FS|"sort|xargs -n4";printf "";close("sort|xargs -n4");delete b}'
1 a1 ,AB ,AC ;AD ,EE
2 a2 ;TR ,WE ,WW ,YT
3 a3 ;AS ;RF ,UY ,YT

thanks everyone for your reply , but i'm not that good in shell , so if i changed the , and ; to another characters , what i should i change in the script ?? i think that's why it's not working ,

thanks

Which characters do you want to remove exactly?

i don't want to remove anything , i just want the same script to order the data , but i changed the special characters that are before the string (, become -)and(;become +)
so what i should change in the script to adopt these changes ??

---------- Post updated at 04:35 AM ---------- Previous update was at 03:41 AM ----------

and btw, the awk that yinyuemi did not worked at all , it has some erros , but the perl script that bartus suggested works but do not give the needed results , it sort some of the data but not all of them.

Try:

perl -nae '@F[2..$#F]=sort {$x=$a;$y=$b;$x=~s/[-+]//;$y=~s/[-+]//;$x cmp $y} @F[2..$#F];print "@F\n"' file
1 Like

Thank you a lot bartus11 , it worked

but i found out that it will add a + sign to the 2nd column , why ?

Can you show some sample data for which this code is producing unwanted output?

+a1 -AB +CE
+a2 -QR -RV
+a3 -UX -WC

---------- Post updated at 06:31 AM ---------- Previous update was at 06:22 AM ----------

ok , i found a way to remove the + sign using cut , btw thanks a lot for your help

Hi.

I often find it cumbersome to craft a specialized sort routine with the builtin perl sort. There is a module that, while not trivial, will make a sorting function for you. Here's an example of that, driven by a shell script:

#!/usr/bin/env bash

# @(#) s4	Demonstrate short, flexible perl code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl

FILE=${1-data1}

pl " perl code:"
cat p1

pl " Input file $FILE:"
cat $FILE

pl "Results:"
./p1 $FILE

exit 0

producing:

% ./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 perl code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate sorting rows, function from Sort::Maker.
# As noted in "Perl Best Practices", p 164 ff.
# http://search.cpan.org/~uri/Sort-Maker-0.06/Sort/Maker.pm

use Sort::Maker;

# Set key as string beginning with second character.
my $sorter = make_sorter( 'ST', string => '/.(\w+)/' );
die "make_sorter: $@" unless $sorter;

# Omit but save fields 1 & 2, key is string beginning with fields 3-.
while (<>) {
  ( $t1, $t2, @a ) = split /\s+/;
  @b = $sorter->(@a);
  print join( " ", $t1, $t2, @b, "\n" );
}

exit(0);

-----
 Input file data1:
1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW 
3 a3 ;AS ,UY ;RF ,YT

-----
Results:
1 a1 ,AB ,AC ;AD ,EE 
2 a2 ;TR ,WE ,WW ,YT 
3 a3 ;AS ;RF ,UY ,YT 

Best wishes ... cheers, drl