Sort on basis of particular field

05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:ANDR.VI:RESTRICTED:S2:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BORE.ST:RESTRICTED:N:

I can sort of the basis of second field using " sort -t: -k2,2 "
but what i want to sort the data is on the basis its extension e.g.: .S .VI .MI .DE .ST

is there any one liner for this. awk etc will do

Thanks a lot in advance

may be this is what you require ?

 
sort -t: -k2,2 t | sort -t . -k 4,4
05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:BORE.ST:RESTRICTED:N:
05.50.25:ANDR.VI:RESTRICTED:S2:

And how the desired output should look like?

05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:BORE.ST:RESTRICTED:N:
05.50.25:ANDR.VI:RESTRICTED:S2:

this is how it should look like

sort -t. -k4 infile

I want a command which will sort on the basis of value between . and :

The solution provided might have issues if there are more dots in the input line after the : which makes this solution prone to error

Could you post an example where the sort command will fail?

Anyway, it's trivial with Perl (not in awk):

perl -e'
  print map $_->[1], 
        sort { 
          $a->[0] cmp $b->[0] 
          } 
        map {
          [ (split/[:.]/)[4], $_ ]
          } <>
          ' infile

The code above will sort the data based on the 5th field.

thanks a lot radoulov

---------- Post updated at 02:09 PM ---------- Previous update was at 02:07 PM ----------

can above formula perform sorting for multiple columns also.
in case yes, can you please let me know the perl script for that also.
the field seperators will be . and : only.

Yes, you'll need to modify the script. To sort by 5 and then by 4 field, for instance, you'll need something like this:

perl -e'
  print map $_->[0],
        sort { 
          $a->[2] cmp $b->[2] ||
          $a->[1] cmp $b->[1]
          } 
        map {
          [ $_, (split /[.:]/)[3..4] ]
          } <>
          ' infile

Yes, just change the first argument passed to split:

split /./
split /:/

thanks a lot radoulov... this is really of great help :slight_smile: