Ex 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Exercise 4

Exercises on grep command

1. What regular expression would a user give for each of the following patterns?

[16pcsa504@linuxserver ~]$ cat file1


hello
I like linux
oops its LINUX
time is 1.35
add this line
1 apple
10 banana
An apple
AAA

a) Either the character 1 or a or B

[16pcsa504@linuxserver ~]$ grep [1aB] file1


time is 1.35
add this line
1 apple
10 banana
An apple

b) Any one alphabet, either upper case or lower case

[16pcsa504@linuxserver ~]$ grep -i L file1


hello
I like linux
oops its LINUX
add this line
1 apple
An apple

c) Any digit at the start of each line

[16pcsa504@linuxserver ~]$ grep ^[0-9] file1


1 apple
10 banana
d) A followed by any character except 2, 9, 0 or a lower case alphabet

[16pcsa504@linuxserver ~]$ grep A[^290a-z] file1


AAA

[16pcsa504@linuxserver ~]$ cat>>file1


ABc
ABcD 2
^C

[16pcsa504@linuxserver ~]$ cat file1


hello
I like linux
oops its LINUX
time is 1.35
add this line
1 apple
10 banana
An apple
AAA
An apple.
ABc
ABcD 2

[16pcsa504@linuxserver ~]$ grep A[^290a-z] file1


AAA
ABc
ABcD 2

e) the character .(full stop) at the end of each line


[16pcsa504@linuxserver ~]$ grep "\.$" file1
An apple.

2. 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 in 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

a) Give the command that would print out the records of all employees whose Last
Name begins with S.

[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
^C
[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 ~]$ grep ^S emp1


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

b) Give the command that would print out the record of the employee whose First
Name is Preethi

[16pcsa504@linuxserver ~]$ grep Preethi emp1


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

c) Give the command that would print out the records whose basic pay is between
1000 and 1999.

[16pcsa504@linuxserver ~]$ grep 1[0-9[0-9][0-9] emp1


Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500

d) Give the command that would print out the records of all employees born in
1960.

[16pcsa504@linuxserver ~]$ grep 60 emp1


Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500

e) Give the command that would print out the records of all employees in the
grades E1 and E2.

[16pcsa504@linuxserver ~]$ grep -e E1 -e E2 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

f) Give the command that would print out the records of all employees in the
Accounts Dept and in Grade E2

[16pcsa504@linuxserver ~]$ grep Accts emp1|grep E2


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

g) Give the command that would print out the records of all employees in the Dept
RND and with 3 years of experience

[16pcsa504@linuxserver ~]$ grep RND emp1|grep 3


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

h) Give the command that would print out the records of all employees whose
employee code is between 3010 and 3059

[16pcsa504@linuxserver ~]$ grep 30[1-5][0-9] emp1


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

i) Give the command that would display the records all employees from Bombay

[16pcsa504@linuxserver ~]$ grep Bombay emp1


Priya~KVS~3005~24K, Raj Road,Bombay~RND~E1~3~06-11-60~1600
Aarthi~N~3500~12, Lake View Road Bombay~MKT~S1~1~1-12-60~1500

j) Give the command that would display only Nirmalas record

[16pcsa504@linuxserver ~]$ grep Nirmala emp1

k) Give the command that will store the records from Delhi in a file called Delhi_wallas.
l) Give the command to print out the record for the employee whose first name is
Jaishankar and the record number along with the record.

[16pcsa504@linuxserver ~]$ grep Jaishankar emp1


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

[16pcsa504@linuxserver ~]$ grep -n Jaishankar emp1


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

m) Give the command to display only the number of employees in Grade E1

[16pcsa504@linuxserver ~]$ grep -c E1 emp1


1

n) Give the command to display all the records except those employees in the MKT
department along with the record number of each

[16pcsa504@linuxserver ~]$ grep -vn MKT emp1


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

You might also like