Scriptome Home
UNIX/Mac Home
Windows Home
Information
FAQ
Help
Overview
Principles
Resources
Tips
Tools
Calc
Change
Choose
Fetch
Merge
Sort
Protocols
Sequences
Microarray

Quickbrowse: Go to a tool by selecting the abbreviated tool name from the menu.

sort_

Contents: Click a blue triangle to expand or collapse a list


Sort lines in a file

To use a script, cut and paste the code from the light green or blue box into a terminal window, change the bold, red text as needed, and hit Enter.

See More Information for notes on using these tools.

Sort a file alphabetically

See General Sorting Notes for details on sorting order.

Sort alphabetically, in ASCENDING order (sort_alpha_asc)

Sort the lines in the file in ascending alphabetical order.

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in ascending alphabetical order\n\n"; print sort @lines ' unsorted > sorted_asc

Example: Sort a gene list. Run the above script on a file called unsorted to get a file called sorted:

Original file (unsorted) Output file (sorted_asc) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 CG12345        9
 CG2500 3
 CG2500 3
 ap23   30
 ap23   7
 cxb7   2
  
 Sorted 6 lines in ascending alphabetical order

Sort alphabetically, in DESCENDING order (sort_alpha_desc)

Sort the lines in the file in descending alphabetical order.

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in descending alphabetical order\n\n"; print sort { $b cmp $a } @lines ' unsorted > sorted_desc

Example: Sort a gene list. Run the above script on a file called unsorted to get a file called sorted_desc:

Original file (unsorted) Output file (sorted_desc) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 cxb7   2
 ap23   7
 ap23   30
 CG2500 3
 CG2500 3
 CG12345        9
  
 Sorted 6 lines in descending alphabetical order

Sort alphabetically, ignoring case

Z and z will count as the same letter in these sorts. See General Sorting Notes for more details on sorting order.

Sort alphabetically, ignoring case, in ASCENDING order (sort_nocase_asc)

Sort the lines in the file in ascending alphabetical order, counting z and Z as the same.

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in ascending alphabetical order, ignoring case\n\n"; print sort { lc($a) cmp lc($b) } @lines ' unsorted > sorted_nc_asc

Example: Sort a gene list. Run the above script on a file called unsorted to get a file called sorted_nc_asc:

Original file (unsorted) Output file (sorted_nc_asc) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 ap23   30
 ap23   7
 CG12345        9
 CG2500 3
 CG2500 3
 cxb7   2
  
 Sorted 6 lines in ascending alphabetical order, ignoring case

Sort alphabetically, ignoring case, in DESCENDING order (sort_nocase_desc)

Sort the lines in the file in descending alphabetical order, counting z and Z as the same.

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in descending alphabetical order, ignoring case\n\n"; print sort { lc($b) cmp lc($a) } @lines ' unsorted > sorted_nc_desc

Example: Sort a gene list. Run the above script on a file called unsorted to get a file called sorted_nc_desc:

Original file (unsorted) Output file (sorted_nc_desc) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 cxb7   2
 CG2500 3
 CG2500 3
 CG12345        9
 ap23   7
 ap23   30
  
 Sorted 6 lines in descending alphabetical order, ignoring case

Sort numerically

See General Sorting Notes for more details on sorting order.

Sort numerically, in ASCENDING order (sort_num_asc)

Sort a simple list numerically.

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in ascending numerical order\n\n"; print sort { $a <=> $b } @lines ' unsorted > sorted_num_asc

Example: Sort a list of numbers. Run the above script on a file called unsorted to get a file called sorted_num_asc:

Original file (unsorted) Output file (sorted_num_asc) Screen Output
 3
 12
 78.5
 -10
 1e5
 1e-3
 -10
 1e-3
 3
 12
 78.5
 1e5
  
 Sorted 6 lines in ascending numerical order

Sort numerically, in DESCENDING order (sort_num_desc)

Input file(s)
Output file

perl -e ' while(<>) { push @lines, $_; } warn "\nSorted $. lines in ascending numerical order\n\n"; print sort { $b <=> $a } @lines ' unsorted > sorted_num_desc

Example: Sort a list of numbers. Run the above script on a file called unsorted to get a file called sorted_num_desc:

Original file (unsorted) Output file (sorted_num_desc) Screen Output
 3
 12
 78.5
 -10
 1e5
 1e-3
 1e5
 78.5
 12
 3
 1e-3
 -10
 
 Sorted 6 lines in ascending numerical order

Sort tabular data, based on the values in a given column

Sort numerically (ascending order) based on a given column (sort_on_column_num)

Sort lines in a tab-separated file in ascending order, based on the numerical value in a given column.

$column Numerical column to sort by
Input file(s)
Output file

perl -e ' $column=1; while(<>) { s/\r?\n//; @F=split /\t/, $_; push @sort_col, $F[$column]; push @lines, "$_\n"; } warn "\nSorted $. lines in ascending order, based on numerical values in column $column\n\n"; print @lines[sort { $sort_col[$a] <=> $sort_col[$b] } 0..$#sort_col] ' unsorted > sorted_col

Example: Sort a gene list based on the score in the first column. Run the above script on a file called unsorted to get a file called sorted_col:

Original file (unsorted) Output file (sorted_col) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 cxb7   2
 CG2500 3
 CG2500 3
 ap23   7
 CG12345        9
 ap23   30
  
 Sorted 6 lines in ascending order, based on numerical values in column 1

Sort numerically (descending order) based on a given column (sort_on_column_num_desc)

Sort lines in a tab-separated file in descending order, based on the numerical values in a given column.

$column Numerical column to sort by
Input file(s)
Output file

perl -e ' $column=1; while(<>) { s/\r?\n//; @F=split /\t/, $_; push @sort_col, $F[$column]; push @lines, "$_\n"; } warn "\nSorted $. lines in descending order, based on numerical values in column 1\n\n"; print @lines[sort { $sort_col[$b] <=> $sort_col[$a] } 0..$#sort_col] ' unsorted > sorted_col

Example: Sort a gene list based on the score in the second, tab-separated column. Run the above script on a file called unsorted to get a file called sorted_col:

Original file (unsorted) Output file (sorted_col) Screen Output
 ap23   7
 ap23   30
 CG2500 3
 cxb7   2
 CG12345        9
 CG2500 3
 ap23   30
 CG12345        9
 ap23   7
 CG2500 3
 CG2500 3
 cxb7   2
  
 Sorted 6 lines in descending order, based on numerical values in column 1

Sort alphabetically (ascending order) based on a given column (sort_on_column_alpha)

Sort lines in a tab-separated file in ascending order, based on the text strings in a given column.

$column Text column to sort by
Input file(s)
Output file

perl -e ' $column=1; while(<>) { s/\r?\n//; @F=split /\t/, $_; push @sort_col, $F[$column]; push @lines, "$_\n"; } warn "\nSorted $. lines in ascending order, based on text strings in column 1\n\n"; print @lines[sort { $sort_col[$a] cmp $sort_col[$b] } 0..$#sort_col] ' unsorted > sorted_col

Example: Sort a gene list based on the gene name in the second, tab-separated column. Run the above script on a file called unsorted to get a file called sorted_col:

Original file (unsorted) Output file (sorted_col) Screen Output
 1      ap23    7
 2      ap23    30
 3      CG2500  3
 4      cxb7    2
 5      CG12345 9
 6      CG2500  3
 5      CG12345 9
 3      CG2500  3
 6      CG2500  3
 1      ap23    7
 2      ap23    30
 4      cxb7    2
  
 Sorted 6 lines in ascending order, based on text strings in column 1

Sort alphabetically (descending order) based on a given column (sort_on_column_alpha_desc)

Sort lines in a tab-separated file in descending order, based on the text strings in a given column.

$column Text column to sort by
Input file(s)
Output file

perl -e ' $column=1; ; while(<>) { s/\r?\n//; @F=split /\t/, $_; push @sort_col, $F[$column]; push @lines, "$_\n"; } warn "\nSorted $. lines in descending order, based on text strings in column 1\n\n"; print @lines[sort { $sort_col[$b] cmp $sort_col[$a] } 0..$#sort_col] ' unsorted > sorted_col

Example: Sort a gene list based on the gene name in the second, tab-separated column. Run the above script on a file called unsorted to get a file called sorted_col:

Original file (unsorted) Output file (sorted_col) Screen Output
 1      ap23    7
 2      ap23    30
 3      CG2500  3
 4      cxb7    2
 5      CG12345 9
 6      CG2500  3
 4      cxb7    2
 1      ap23    7
 2      ap23    30
 3      CG2500  3
 6      CG2500  3
 5      CG12345 9
  
 Sorted 6 lines in descending order, based on text strings in column 1

Other Sorting

Change order of columns in tabular data

See Choose columns in a given order (choose_cols) in the Choose manpage.


More Information

General Sorting Notes

The tools as written will sort the given file1 and send the result to the given output file. In fact, any number of input files can be used (before the '>' sign), but just one output file. Change the filenames as you wish.

Sorting order: Note that 100 will come before 20 when sorting alphabetically, because 1 comes before 2. Also, lower-case letters come after all of the upper-case letters (i.e., a will come after Z), unless case-sensitive sorting is used. (i.e., when sorting in ascending order, "arabidopsis" will come after "Zebrafish"), unless case-insensitive sorting is used. See the examples below for demonstrations of these rules.

Duplicates will not be eliminated. That is, if the same line appears five times in a file, it will appear five times (in a row) in the output file.

General Scriptome Notes

Scriptome tools are in blue or green boxes. Cut and paste the text of the tool into a terminal window. Then edit the line as needed. Things that will often need to be edited are highlighted in red. Input and output filenames will almost always need to be changed.

All scripts that work on tabular data assume the data is tab-separated. Use a Change script to change, e.g., comma-separated data to tab-separated before using these scripts.

 

HomeContact UsDirectoriesSearch