Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views7 pages

RHEL 7 String Manipulation Sort Command

The SORT command in Linux/Unix is a utility for sorting lines of text files in various orders, including alphabetically, numerically, and in reverse. It supports multiple options such as -o for output file specification, -r for reverse sorting, -n for numeric sorting, and -u for removing duplicates. The command can also sort based on specific fields and delimiters, making it versatile for handling different types of data files.

Uploaded by

abhisheksafeeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

RHEL 7 String Manipulation Sort Command

The SORT command in Linux/Unix is a utility for sorting lines of text files in various orders, including alphabetically, numerically, and in reverse. It supports multiple options such as -o for output file specification, -r for reverse sorting, -n for numeric sorting, and -u for removing duplicates. The command can also sort based on specific fields and delimiters, making it versatile for handling different types of data files.

Uploaded by

abhisheksafeeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Sort Linux and Unix Commands

SORT command in Linux/Unix with examples


SORT command is used to sort a file, arranging the records in a
particular order. By default, the sort command sorts file assuming the
contents are ASCII. Using options in sort command, it can also be used
to sort numerically.

SORT command sorts the contents of a text file, line by line.


sort is a standard command line program that prints the lines of its
input or concatenation of all files listed in its argument list in
sorted order.
The sort command is a command line utility for sorting lines of text
files. It supports sorting alphabetically, in reverse order, by
number, by month and can also remove duplicates.
The sort command can also sort by items not at the beginning of the
line, ignore case sensitivity and return whether a file is sorted or
not. Sorting is done based on one or more sort keys extracted from
each line of input.
By default, the entire input is taken as sort key. Blank space is the
default field separator.

The sort command follows these features as stated below:

Lines starting with a number will appear before lines starting with a
letter.
Lines starting with a letter that appears earlier in the alphabet will
appear before lines starting with a letter that appears later in the
alphabet.
Lines starting with a lowercase letter will appear before lines
starting with the same letter in uppercase.
1 .When we have a mix file with both uppercase and lowercase letters
then first the lower case letters would be sorted following with the
upper case letters .

[agoutam@rhel7 ~]$ sort str


aix
hpux
Linux
Linux
php
Solaris
Unix
[agoutam@rhel7 ~]$

Option

-o Option : built-in sort option -o, which allows you to specify


an output file
Using the -o option is functionally the same as redirecting the
output to a file
sort file.txt > output.txt

-r Option: the -r flag is an option of the sort command which sorts


the input file in reverse order i.e. descending order by default.

Syntax :
$ sort -r inputfile.txt
[agoutam@rhel7 ~]$ sort -r str
Unix
Solaris
php
Linux
Linux
hpux
aix
[agoutam@rhel7 ~]$~]$

-n Option : To sort a file numerically used –n option. This option is


used to sort the file with numeric data present inside.

[agoutam@rhel7 ~]$ sort -n num


20
30
45
56
78
87
90
[agoutam@rhel7 ~]$

-nr option : To sort a file with numeric data in reverse order


Syntax :
$ sort -nr filename.txt

[agoutam@rhel7 ~]$ sort -nr num


90
87
78
56
45
30
20
[agoutam@rhel7 ~]$

-k Option : Unix provides the feature of sorting a table on the basis


of any column number by using -k option.

Syntax :

$ sort -k filename.txt
[agoutam@rhel7 ~]$ sort -k 2n strnum
Unix 34 agoutam ARICENT PRESIDENT
Linux 43 jadeja accenture TESTER
Linux 45 VIJAY comverse CEO
aix 76 kohli tcs LINUX
Solaris 90 ravish AMDOCS manager
hpux 654 RAVI Amazon developer
[agoutam@rhel7 ~]$

 '-t' option is used to provide the delimiter in case of files


with delimiter. '-k' is used to specify the keys on the basis of
which the sorting has to be done. The format of '-k' is : '-km,n'
where m is the starting key and n is the ending key

 if the sorting is to be done on the basis of first 3 fields, it


will be: '-k 1,3'.

 Note: For a file which has fields delimited by a space or a tab,


there is no need to specify the "-t" option since the white space
is the delimiter by default in sort.

Input file strnum Now modified to contain Duplicate in first column


and field is separated by delimiter ,(comma)

[agoutam@rhel7 ~]$ cat strnum


Unix,34,agoutam,ARICENT,PRESIDENT
Linux,45,VIJAY,comverse,CEO
Solaris,90,ravish,AMDOCS,manager
aix,76,kohli,tcs,LINUX
Linux,43,jadeja,accenture,TESTER
hpux,654,RAVI,Amazon,developer

sorting file on the basis of 2nd field , numerically:


$ sort -t"," -k2n,2 file

[agoutam@rhel7 ~]$ sort -t"," -k2n,2 strnum


Unix,34,agoutam,ARICENT,PRESIDENT
Linux,43,jadeja,accenture,TESTER
Linux,45,VIJAY,comverse,CEO
aix,76,kohli,tcs,LINUX
Solaris,90,ravish,AMDOCS,manager
hpux,654,RAVI,Amazon,developer
[agoutam@rhel7 ~]$
-u option : To sort and remove duplicates pass the -u option to sort.
This will write a sorted list to standard output and remove duplicates

Remove duplicates from the file based on 1st field:


$ sort -t"," -k1,1 -u file

[agoutam@rhel7 ~]$ sort -t"," -k1,1 -u strnum


aix,76,kohli,tcs,LINUX
hpux,654,RAVI,Amazon,developer
Linux,45,VIJAY,comverse,CEO
Solaris,90,ravish,AMDOCS,manager
Unix,34,agoutam,ARICENT,PRESIDENT
[agoutam@rhel7 ~]$

sort the file alphabetically on the 1st field, numerically on the 2nd
field:

[agoutam@rhel7 ~]$ sort -t"," -k1,1 -k2n,2 strnum


aix,76,kohli,tcs,LINUX
hpux,654,RAVI,Amazon,developer
Linux,43,jadeja,accenture,TESTER
Linux,45,VIJAY,comverse,CEO
Solaris,90,ravish,AMDOCS,manager
Unix,34,agoutam,ARICENT,PRESIDENT
[agoutam@rhel7 ~]$

15. sort a file based on the 1st and 3rd field, and numerically on 2rd
field on a file containing 5 columns:

$ sort -t"," -k1,3 -k3n,3 file


[agoutam@rhel7 ~]$ sort -t"," -k1,3 -k3n,3 strnum
aix,76,kohli,tcs,LINUX
hpux,654,RAVI,Amazon,developer
Linux,43,jadeja,accenture,TESTER
Linux,45,VIJAY,comverse,CEO
Solaris,90,ravish,AMDOCS,manager
Unix,34,agoutam,ARICENT,PRESIDENT
[agoutam@rhel7 ~]$

-c option : This option is used to check if the file given is already


sorted or not & checks if a file is already sorted pass the -c option
to sort

Note : If there is no output then the file is considered to be already


sorted
[agoutam@rhel7 ~]$ cat stroutput.txt
aix
hpux
Linux
Linux
php
Solaris
Unix
[agoutam@rhel7 ~]$
[agoutam@rhel7 ~]$ sort -c stroutput.txt
[agoutam@rhel7 ~]$
[agoutam@rhel7 ~]$ sort stroutput.txt
aix
hpux
Linux
Linux
php
Solaris
Unix
[agoutam@rhel7 ~]$

Sort, merge and remove duplicates:


$ sort -nu file1 file2
Application and uses of sort command

 It can sort any type of file be it table file text file numeric
file and so on.
 Sorting can be directly implemented from one file to another
without the present work being hampered.
 Sorting of table files on the basis of column has been made way
simpler and easier.
 So many option are available for sorting in all possible ways.
 The most beneficial use is that a particular data file can be
used many times as no change is made in the input file provided.
 Original data is always safe and not hampered.

You might also like