Sort data As per first Column

hI

I have file A

NSU30504 5 6 G 6  
NSU3050B T 7 9 J
NSU30506 T I 8 9 
NSU3050C H J K L 

Output:

NSU3050B T 7 9 J
NSU3050C H J K L 
NSU30504 5 6 G 6  
NSU30506 T I 8 9

If I understand the question correctly, with recent GNU sort implementations:

sort -V infile
1 Like

simply sort it ..

sort -n FileA

it's not helping me ...

I am using shell scripting//

Solaris...

---------- Post updated at 04:17 AM ---------- Previous update was at 03:49 AM ----------

Data should be short as per column A (First Column)

Try:

perl -lane'
  push @d, [$_, $F[0] =~ /(\D)+$/, "z"];
  END {
    print join $/, map $_->[0], sort {
      $a->[1] cmp $b->[1] ||
        $a->[0] <=> $b->[0]
      } @d
    }' infile

You actually mean sort (not short), don't you?

Its working and also i want sort my data as per column B if it is possible ?

Input:

NSU30504 5 6 G 6  
NSU3050B 1 7 9 J
NSU30506 4 I 8 9 
NSU3050C 9 J K L

Output:

NSU3050B 1 7 9 J
NSU30506 4 I 8 9 
NSU30504 5 6 G 6 
NSU3050C 9 J K L

Thanks

---------- Post updated at 04:47 AM ---------- Previous update was at 04:37 AM ----------

Sorry man sort data in ascending order//

OK,
try this (use the value of $c to set the column (note that the first column is 0).

This will sort by the second column:

perl -lane'
  $c = 1;
  ($w = $F[$c]) =~ s/\d/z/g;
  ($d = $F[$c]) =~ s/\D/0/g;
  push @data, [$_, $w, $d];
  END {
    print join $/, map $_->[0], sort {
      $a->[1] cmp $b->[1] ||
        $a->[2] <=> $b->[2]
      } @data
    }' infile
1 Like

Good man its working fine .....but i need one for thing to add..

It is possible can we sort first column and after second column.

Just EX:

NSU3051 NSU305011 5 6 G 6  
NSU3051 NSU305009 1 7 9 J
NSU3050 NSU3050120 4 I 8 9 
NSU3050 NSU30501210 9 J K L
NSU3050 NSU3050120 4 I 8 9 
NSU3050 NSU30501210 9 J K L
NSU3051 NSU305009 1 7 9 J
NSU3051 NSU305011 5 6 G 6 

Once again thanks a lot for your help ....

perl -lane'
  @w = @d = ();
  for (0..1) {
    ($w = $F[$_]) =~ s/\d/z/g;
    ($d = $F[$_]) =~ s/\D/0/g;
    push @w, $w;
    push @d, $d;
    }
  push @data, [$_, [@w], [@d]];
  END {
    print join $/, map $_->[0], sort {
      $a->[1][0] cmp $b->[1][0] ||
        $a->[2][0] <=> $b->[2][0] ||
          $a->[1][1] cmp $b->[1][1] ||
            $a->[2][1] <=> $b->[2][1]          
      } @data
    }' infile

Guys,

why not this.?

sort -k 1 -k 2 File

Cheers,
Ranga:-)

Because the values may contain alpha characters (please read the entire thread).

Consider the following input:

NSU305B NSU305011 5 6 G 6  
NSU3051 NSU305009 1 7 9 J
NSU305C NSU3050120 4 I 8 9 
NSU3050 NSU30501210 9 J K L

Your command will return:

NSU3050 NSU30501210 9 J K L
NSU3051 NSU305009 1 7 9 J
NSU305B NSU305011 5 6 G 6  
NSU305C NSU3050120 4 I 8 9 

Mine:

NSU305B NSU305011 5 6 G 6  
NSU305C NSU3050120 4 I 8 9 
NSU3050 NSU30501210 9 J K L
NSU3051 NSU305009 1 7 9 J

Hi.

Utility msort allows a key type hybrid composed of alphabetic and numeric characters. Here is an example. The script is long, but it's primarily infrastructure to display and compare files, so look at the heart of it -- the msort command in Section 2:

#!/usr/bin/env bash

# @(#) s1	Demonstrate hybrid sort, msort.
# http://freecode.com/projects/msort

# Section 1, setup, pre-solution, $Revision: 1.25 $".
# Infrastructure details, environment, debug commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin" HOME=""
set +o nounset
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { : ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
C=$HOME/bin/context && [ -f $C ] && $C msort
set -o nounset
pe

FILE=${1-data1}
shift
E=${1-expected-output.txt}

# Display sample of data file, with head & tail as a last resort.
# NB, input, expected-output trimmed trailing blanks.
db " Section 1: display of input data."
pe " || start sample [ specimen first:middle:last ] $FILE"
specimen $FILE $E 2>/dev/null \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

# Section 2, solution.
pl " Results:"
db " Section 2: solution."
msort -q -j -l -n 1 -c h $FILE |
tee f1

# Section 3, post-solution, check results, clean-up, etc.
v1=$(wc -l <$E)
v2=$(wc -l < f1)
pl " Comparison of $v2 created lines with $v1 lines of desired results:"
db " Section 3: validate generated calculations with desired results."

pl " Comparison with desired results:"
if [ ! -f $E -o ! -s $E ]
then
  pe " Comparison file \"$E\" zero-length or missing."
  exit
fi
if cmp $E f1
then
  pe " Succeeded -- files have same content."
else
  pe " Failed -- files not identical -- detailed comparison follows."
  if diff -b $E f1
  then
    pe " Succeeded by ignoring whitespace differences."
  fi
fi

exit 0

producing:

% ./s1

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) 
bash GNU bash 3.2.39
msort 8.44

 db,  Section 1: display of input data.
 || start sample [ specimen first:middle:last ] data1
Whole: 5:0:5 of 4 lines in file "data1"
NSU30504 5 6 G 6
NSU3050B T 7 9 J
NSU30506 T I 8 9
NSU3050C H J K L

Whole: 5:0:5 of 4 lines in file "expected-output.txt"
NSU3050B T 7 9 J
NSU3050C H J K L
NSU30504 5 6 G 6
NSU30506 T I 8 9
 || end

-----
 Results:
 db,  Section 2: solution.
NSU3050B T 7 9 J
NSU3050C H J K L
NSU30504 5 6 G 6
NSU30506 T I 8 9

-----
 Comparison of 4 created lines with 4 lines of desired results:
 db,  Section 3: validate generated calculations with desired results.

-----
 Comparison with desired results:
 Succeeded -- files have same content.

Note that the data files were trimmed of trailing blanks.

The second data file has an additional hybrid key, field 2. Here are the results:

% ./s2

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) 
bash GNU bash 3.2.39
msort 8.44

 db,  Section 1: display of input data.
 || start sample [ specimen first:middle:last ] data2
Whole: 5:0:5 of 4 lines in file "data2"
NSU3051 NSU305011 5 6 G 6
NSU3051 NSU305009 1 7 9 J
NSU3050 NSU3050120 4 I 8 9
NSU3050 NSU30501210 9 J K L

Whole: 5:0:5 of 4 lines in file "expected-2"
NSU3050 NSU3050120 4 I 8 9
NSU3050 NSU30501210 9 J K L
NSU3051 NSU305009 1 7 9 J
NSU3051 NSU305011 5 6 G 6
 || end

-----
 Results:
 db,  Section 2: solution.
NSU3050 NSU3050120 4 I 8 9
NSU3050 NSU30501210 9 J K L
NSU3051 NSU305009 1 7 9 J
NSU3051 NSU305011 5 6 G 6

-----
 Comparison of 4 created lines with 4 lines of desired results:
 db,  Section 3: validate generated calculations with desired results.

-----
 Comparison with desired results:
 Succeeded -- files have same content.

The msort code was in my Debian repository, but it can be acquired from the website noted in the script.

Best wishes ... cheers, drl

1 Like

That will result in :

pandeeswaran@ubuntu:~$ sort -n file_a
NSU30504 5 6 G 6  
NSU30506 T I 8 9 
NSU3050B T 7 9 J
NSU3050C H J K L