Skip to main content

Sorting a file in unix

 

Sorting a file  in Unix

Sort command in unix or linux system is used to order the elements or text. Sort command has the capability of sorting numerical values and strings. The sort command can order the lines in a text file.

The syntax of sort command is:


sort [options] filename


The options are:


-b : Ignores leading spaces in each line

-d : Uses dictionary sort order. Conisders only spaces and alphanumeric characters in sorting

-f : Uses case insensitive sorting.

-M : Sorts based on months. Considers only first 3 letters as month. Eg: JAN, FEB

-n : Uses numeric sorting

-R : Sorts the input file randomly.

-r : Reverse order sorting

-k : Sorts file based on the data in the specified field positions.

-u : Suppresses duplicate lines

-t : input field separator


Sort Command Examples:

Before practicing the examples create the below two files in your unix system:


> cat order.txt

Unix distributed 05 server

Linux virtual 3 server

Unix distributed 05 server

Distributed processing 6 system

 

> cat delim_sort.txt

Mayday|4

Janmon|1

Declast|12


1. Sorting lines of text

The default sort command uses alphabetical order (ASCII order) to sort the file. It treats each line as a string and then sorts the lines.


> sort order.txt

Distributed processing 6 system

Linux virtual 3 server

Unix distributed 05 server

Unix distributed 05 server


2. Sorting based on the field positions.

You can specify the field postions using the -k option of sort command. The sort command uses the space or tab as the default delimiter. To sort based on the data in the second field, run the below command:


> sort -k2 order.txt

Unix distributed 05 server

Unix distributed 05 server

Distributed processing 6 system

Linux virtual 3 server


You can also pecify more than field with k option as a comma separated list. The below command uses the second and fourth fields to sort the data.


> sort -k2,4 order.txt


3. Numeric sorting

Instead of the default alphabetical sorting order, you can make the sort command to sort in numeric order using the -n option. This is shown below:


> sort -nk3 order.txt

Linux virtual 3 server

Unix distributed 05 server

Unix distributed 05 server

Distributed processing 6 system


4. Sort in reverse order

By default, the sort command sorts the data in ascending order. You can change this to descending order using the -r option.


> sort -nrk3 order.txt

Distributed processing 6 system

Unix distributed 05 server

Unix distributed 05 server

Linux virtual 3 server


5. Suppressing duplicates or Print only unique values

You can produce only unique values in the output using the - u option of the sort command.


> sort -u order.txt

Distributed processing 6 system

Linux virtual 3 server

Unix distributed 05 server


Another way is piping the output of sort command to uniq command.


> sort order.txt | uniq


6. Delimited file input

In the second, third and fourth examples we have sorted the data based on the field positions. Here the fields are separted by space or tab character. What if the fields are specifed by any other character? In such cases, we have to specify the input delimiter with the -t option. An example is shown below:


> sort -t'|' -nrk2 delim_sort.txt

Declast|12

Mayday|4

Janmon|1


7. Sorting on months.

We can sort the data in the monthwise using the -M option of the sort command. This is shown below:


> sort -M delim_sort.txt

Janmon|1

Mayday|4

Declast|12


Treats the first 3 characters in the string as month and then sorts in months order.

 

Comments

Popular posts from this blog

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C   NUMBER(3); BEGIN WHILE B <

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7     1.      Introduction This document outlines a mini project for the RDBMS LOT

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,         TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600,   300, 30); INSERT INTO EMP VALUES         (7521, 'WARD',    'SALESMAN',   7698,         TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250,   500, 30); INSERT INTO EMP VALUES         (7566, 'JONES',   'MANAGER',    7839,         TO_DATE('2-APR-1981', 'DD-MON-YYYY'),   2975, NULL,