Sort data As per first Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort data As per first Column
# 8  
Old 02-26-2012
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:

Code:
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

This User Gave Thanks to radoulov For This Post:
# 9  
Old 02-26-2012
Bug

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:

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

Code:
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 ....
# 10  
Old 02-26-2012
Code:
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

# 11  
Old 02-26-2012
Sort

Guys,

why not this.?
Code:
sort -k 1 -k 2 File

Cheers,
Ranga:-)
# 12  
Old 02-26-2012
Quote:
Originally Posted by rangarasan
Guys,

why not this.?
Code:
sort -k 1 -k 2 File

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

Consider the following input:

Code:
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:

Code:
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:

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


Last edited by radoulov; 02-26-2012 at 08:10 AM..
# 13  
Old 02-26-2012
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:
Code:
#!/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:
Code:
% ./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:
Code:
% ./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
This User Gave Thanks to drl For This Post:
# 14  
Old 02-26-2012
Quote:
Originally Posted by codemaniac
simply sort it ..

Code:
sort -n FileA

That will result in :
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

2. UNIX for Beginners Questions & Answers

How do I sort data in column properly?

Can i use 'column' command to get the required 3rd column output? Input example: 1 2 345678 90 2 2 356 42 3 3 8265 55 Output required: 1 2 345678 90 2 2 356 42 3 3 8265 55 Basically i want the 3rd column to be justified to the right, instead of left.... (3 Replies)
Discussion started by: nurul_nadzirah
3 Replies

3. Shell Programming and Scripting

Help with sort only column 2 data separately

Input File Contig_1_294435nt 242231 242751 Contig_1_294435nt 242390 242782 Contig_1_294435nt 242390 242782 Contig_1_294435nt 291578 291668 Contig_2_242278nt 75910 76271 Contig_2_242278nt 76036 76316 Contig_2_242278nt 76036 76316... (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Sort data by date and then search by column

Hi, I have a file where data is pipe separated.First i want to sort the file content by date . Then i want to pick up the records based on the first column which should be unique and not have duplicates. NYSE|yyyrrrddd|toronto|isin|ticker|2013-05-15... (2 Replies)
Discussion started by: samrat dutta
2 Replies

5. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Advanced: Sort, count data in column, append file name

Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out. I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies

7. Shell Programming and Scripting

Sort data from column to row

Hi, I need somebody's help with sorting data with awk. I've got a file: 10 aaa 4584 12 bbb 6138 20 ccc 4417 21 ddd 7796 10 eee 7484 12 fff ... (5 Replies)
Discussion started by: killerbee
5 Replies

8. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

9. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question