Vous êtes sur la page 1sur 4

EX-5: Cut, Head, Tail

I. Use the empdata.doc. Here the ~ character is used as the field separator.
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

1. Give the command to display the employee first name, last name, employee code,
department code and grade from the file.

[16pcsa504@linuxserver ~]$ cat emp1


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

[16pcsa504@linuxserver ~]$ cut -d\~ -f1,2,3,5,6 emp1


Priya~KVS~3005~RND~E1
Anne~Preethi~3010~Accts~E2-3
Aarthi~N~3500~MKT~S1
Sanjana~Jaishankar~3002~Accts~M1

2. Give the command to display the first 4 fields.

[16pcsa504@linuxserver ~]$ cut -d\~ -f1-4 emp1


Priya~KVS~3005~24K, Raj Road,Bombay
Anne~Preethi~3010~10, Annie Besant Road Chennai
Aarthi~N~3500~12, Lake View Road Bombay
Sanjana~Jaishankar~3002~14, Hill View Apts, Adyar, Chennai
3. Give the command to display the 2nd , 3rd , 4th fields

[16pcsa504@linuxserver ~]$ cut -d\~ -f2-4 emp1


KVS~3005~24K, Raj Road,Bombay
Preethi~3010~10, Annie Besant Road Chennai
N~3500~12, Lake View Road Bombay
Jaishankar~3002~14, Hill View Apts, Adyar, Chennai

4. Give the command to display certain columns within field(s).

[16pcsa504@linuxserver ~]$ cut -c1-4 emp1


Priy
Anne
Aart
Sanj

II. Create a file called trans.doc. The field separator is a blank character. The file consists
of the following fields:

Transaction Number, Customer Name, Customer Code, Product code of product sold,
Number of units sold, Rate per unit

[16pcsa504@linuxserver ~]$ cat>trans.doc


1001 Bavani 515 10 2 100
10021 Haseena 503 52 1 251
1003 Pavi 514 85 1 1500
1004 Deepthikavi 151 33 9 25
1018 Roshan 520 10 15 100
^C
[16pcsa504@linuxserver ~]$ cat trans.doc
1001 Bavani 515 10 2 100
10021 Haseena 503 52 1 251
1003 Pavi 514 85 1 1500
1004 Deepthikavi 151 33 9 25
1018 Roshan 520 10 15 100
[16pcsa504@linuxserver ~]$ cut -c1-4 emp1
Priy
Anne
Aart
Sanj
1. Give the command to display the fields transaction number, customer code and Rate of
item

[16pcsa504@linuxserver ~]$ cut -d' ' -f1,3,6 trans.doc


1001 515 100
10021 503 251
1003 1
1004 151 25
1018 520 100

2. Give the command to store the customer name, customer code and units sold to the
customer in a file called cust.dat

[16pcsa504@linuxserver ~]$ cut -d' ' -f2,3,5 trans.doc > cust.dat


[16pcsa504@linuxserver ~]$ cat cust.dat
Bavani 515 2
Haseena 503 1
Pavi 85
Deepthikavi 151 9
Roshan 520 15

3.Give the command to display the first 5 fields from the file

[16pcsa504@linuxserver ~]$ cut -d' ' -f1-5 trans.doc


1001 Bavani 515 10 2
10021 Haseena 503 52 1
1003 Pavi 514 85
1004 Deepthikavi 151 33 9
1018 shan 520 10 15

4. Give the command to display all fields after the 2nd field.

[16pcsa504@linuxserver ~]$ cut -d' ' -f2- trans.doc


Bavani 515 10 2 100
Haseena 503 52 1 251
Pavi 514 85 1 1500
Deepthikavi 151 33 9 25
Roshan 520 10 15 100
III) Determine the number of lines, words, characters in your file empdata.doc

[16pcsa504@linuxserver ~]$ wc -l emp1


4 emp1
[16pcsa504@linuxserver ~]$ wc -w emp1
19 emp1
[16pcsa504@linuxserver ~]$ wc -c emp1
278 emp1

[16pcsa504@linuxserver ~]$ wc -lc emp1


4 278 emp1
[16pcsa504@linuxserver ~]$ wc -wc emp1
19 278 emp1

Vous aimerez peut-être aussi