Você está na página 1de 4

EX-6: Sort commands

1. For the following exercises, the file empdata should be used. The fields in the file are as
follows:
Employee last name Starts with an upper case alphabet
Employee first name Starts with an upper-case alphabet
Employee code Numeric in the range 3000 3999
Permanent address In upper case and lower case
Department code Codes are RND, Accts, MKT
Grade Grades are E1, E2, S1, S2, M1, M2
Years of Experience Numeric, 1 or 2 digits
Date of Birth dd-mm-yy format
Basic Pay Numeric, 4 digits

~ is the field separator.


Data file

Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600


Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-
85~2200
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600

1. Sort the file by Employee Last name and display the results on the VDU.

[16pcsa504@linuxserver ~]$ cat > empdata


Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600

[16pcsa504@linuxserver ~]$ cat empdata


Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
[16pcsa504@linuxserver ~]$ sort -t\~ -k1 empdata

Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500


Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200

2. Sort the contents of the file by Dept code and store the contents in the same file.

[16pcsa504@linuxserver ~]$ sort -t\~ -k5 empdata

Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000


Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600

[16pcsa504@linuxserver ~]$ sort -t\~ -k5 empdata -o empdata

[16pcsa504@linuxserver ~]$ cat empdata

Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000


Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600

3. Sort the contents of the file by Dept code and store the contents in a file called dept_wise.

[16pcsa504@linuxserver ~]$ sort -t\~ -k5 empdata -o dept_wise

[16pcsa504@linuxserver ~]$ cat dept_wise

Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000


Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
4. Give the command to arrange the contents of the file in order of Department code within which
in order of grade and store the details in the file deptgr_wise.

[16pcsa504@linuxserver ~]$ sort -t\~ -k5,6 empdata -o deptgr_wise


[16pcsa504@linuxserver ~]$ cat deptgr_wise

Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000


Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600

5. Give the command to arrange the contents of the file in increasing order of Years of experience
and store the details in the file exp.dat.

[16pcsa504@linuxserver ~]$ sort -t\~ -k7 empdata -o exp.dat


[16pcsa504@linuxserver ~]$ cat exp.dat

Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500


Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200

6.Give the command to arrange the contents of the file in decreasing order of Years of experience
and display the details on the VDU.

[16pcsa504@linuxserver ~]$ sort -t\~ -k7 -r empdata -o exp.dat


[16pcsa504@linuxserver ~]$ cat exp.dat

Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200


Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500

7. Give the command to sort the file by Department code , within which by years of experience and
display the output on the screen.

[16pcsa504@linuxserver ~]$ sort -t\~ -k5,7 empdata

Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000


Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600

8. Give the command to sort the file by reverse order of Grade within which by decreasing order of
Basic Pay and store the output in empdata.(empdatt).

[16pcsa504@linuxserver ~]$ sort -t\~ -rk6,9 empdata


Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600

[16pcsa504@linuxserver ~]$ sort -t\~ -k6,9 empdata -o empdatt


[16pcsa504@linuxserver ~]$ cat empdatt

Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600


Prema~A~3006~12, Raj Bhavan,Chennai~RND~E1~3~06-11-60~2600
Anne~Preethi~3010~10, Annie Besant Road Chennai~Accts~E2-3~21-12-70~2000
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai~Accts~M1~5~12-12-85~2200
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500

Você também pode gostar