Sunday, February 2, 2014

Linux Grep Command

Create a file of yellow pages with details of cutomer name , address , phone number,date . Perform
the following command for the above file.
1. 1. display the line that having phone number with extension “x” and end with 4 digits” ex:
(x1234)
2. extract the date using the format of ddmmyear.
3. Display the line the customer name do not end with “s”
4. To print two lines after the pattern searched regarding to the customer address.
5. Display the customer name which starts with “y”
6. Display the count of the customers situated in madurai.
7. Display the line that having customer name “vinai” not using grep / egrep command by
8. Print the matching line along with the line number.
9. Print the line donot have the city as chennai.
10. Dipslay the line with zero byte with any pattern searching.
ignoring case.
Answer:
1. egrep “x.{4}$” filename
2. egrep “[az]{3}[][09]{1,2}” filename
3. grep e '[^s]$' filename
4. grep A 2 “pattern” filename
5. grep “^y” filename
6. grep c “madurai” filename
7. look f “vinai” filename
8. grep n “pattern” filename
9. grep v “pattern” filename
10. grep z “patern” filename


Create a student details of various department with Fields (studentname, regno, marks,
Percentage). Perform the following commands based on the above file.
1. Print all the student name containing a vowel (a, e, i, o, or u) followed by a single character
followed by the same vowel again. Thus, it will find “eve” or “adam” but not “vera”.
2. Print the line that’s having upper case letters of student name.
3. Print 2 lines before the matching pattern
4. Display only the student name start with “a” and end with “i”
5. Display the student details having mark of 100
6. Display computer science student name alone.
7. Extract 2011 batch student Regno for all the departments.
8. Print the student detail who doesn’t having the 80% .
9. Display the count of student s having name “Jeya”
10. Display the student details name starts with “L” or “T” (no case sensitive).
egrep "[11]{2}[az]{4}[09]{2}" stu
saran |11pite02|100
Answer:
1. egrep "^[aeiou]{1}[az]{1}[aeiou]{1}” file1
2. egrep '[[:upper:]]' filenmae
3. grep B 2 “patterrn” filename
4. grep “^a . * i$” filenmae
5. grep 100 fnanme
6. egrep "pite" stu|cut d"|" f1
7. cut d"|" f2 stu|egrep ^11
8. grep – v 80
9. grep –c “pattern” fname
10. egrep ‘^[LT].*’ fname
Create two files as given below
Water pollution -
• Water pollution is one of the most serious environmental problems.
• It occurs when water is contaminated by such substances as human and animal wastes, toxic
   industrial chemicals, agricultural residues, oil and heat.
• Most of our water bodies— rivers, lakes, seas, oceans, estuaries and underground water sources
     (i.e., tubwells, bore wells) are gradually becoming polluted.
• In the course "Covered so far, you have seen how deforestation, urbanization, intensive
agriculture and industrialization have caused pollution of water bodies.
•Air Pollution
• Air pollution is a common problem in our country.
• Air pollution comes from dangerous chemical substances which spread in the air, for example,
carbon monoxide, CFC, carbon dioxide, hydro carbon, sulfur dioxide, etc.
• Those substances are produced by human activities such as the using of vehicles which produce
smokes causing the air pollution and factory activities such as producing the tires and fake animal
skin which use fuels and machine that trigger the air pollution.
• However, most people are lack of attention to this problem. Even, they do not care that air
pollution can be very dangerous for the human health and our environment.
1. print the filename itself which is having the pattern people.
2. Print the matching lines from both the files for the pattern “pollution”
3. Print the matching lines along with line number from both the files for the pattern “human”
(ignorecase).
4. Print the 1 line above and below the matching pattern “animal” from both files.
5. Print the common pattern from file1 and file2.
6. Display line for that pattern starts with “C” and ends with “y” for “Air pollution file”
7. Search for the pattern “country” in file1 and file2 and suppress the error message.
8. Search for the full line content in the file1
9. Print the line which do not have the word “is” in file1
10. Print line which is start with the word “Air” (ignore case)
Answer:
1.grep –l people f1 f2
2. grep –h “pollution” f1 f2
3. grep –n i “human” f1 f2
4. grep –C 1 “animal” f1 f2
5. grep –f f1 f2
6. egrep ‘^C.*y$’ f2
7. grep –s “country” f2
8. grep –X “sdfafds” f1
9. grep –v “is” f1
10. look -f “Air” f2